From 537d5b8e495f6f6ab62d279f35bfbae818c6b165 Mon Sep 17 00:00:00 2001 From: Nadim Salloum Date: Wed, 16 Jun 2021 19:14:32 +0300 Subject: [PATCH] brand filter for cars --- app/Http/Controllers/CarController.php | 4 ++-- app/Models/Car.php | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/CarController.php b/app/Http/Controllers/CarController.php index d00c6ce..1b46904 100644 --- a/app/Http/Controllers/CarController.php +++ b/app/Http/Controllers/CarController.php @@ -191,12 +191,12 @@ class CarController extends Controller $cars = $this->getWithCustomSort($cars, $sortBy, $direction); return Inertia::render($renderPage, [ - 'filters' => $request->all('search', 'trashed'), + 'filters' => $request->all('search', 'trashed', 'brand'), 'sort' => [ 'by' => $sortBy, 'direction' => $direction, ], - 'cars' => $cars->filter($request->only('search', 'trashed')) + 'cars' => $cars->filter($request->only('search', 'trashed', 'brand')) ->paginate(50) ->withQueryString() ->through(fn ($car) => [ diff --git a/app/Models/Car.php b/app/Models/Car.php index 7d662ec..cfb7f9d 100644 --- a/app/Models/Car.php +++ b/app/Models/Car.php @@ -124,7 +124,7 @@ class Car extends Model public function scopeSoldOnly($query) { - return $query->withContractCount()->having('sell_contracts_count', '>', 0)->havingRaw('buy_contracts_count <= sell_contracts_count'); + return $query->withContractCount()->having('sell_contracts_count', '>', 0)->havingRaw('buy_contracts_count = sell_contracts_count'); } public function scopeFilter($query, array $filters) @@ -150,6 +150,12 @@ class Car extends Model } elseif ($trashed === 'only') { $query->onlyTrashed(); } + })->when($filters['brand'] ?? null, function ($query, $brand) { + $query->whereHas('carModel', function($q) use ($brand) + { + $q->where('brand_id', '=', $brand); + + })->get(); }); } }