brand filter for cars

shift-build-2464
Nadim Salloum 2021-06-16 19:14:32 +03:00
parent 46d0dc7ad8
commit 537d5b8e49
2 changed files with 9 additions and 3 deletions

View File

@ -191,12 +191,12 @@ class CarController extends Controller
$cars = $this->getWithCustomSort($cars, $sortBy, $direction); $cars = $this->getWithCustomSort($cars, $sortBy, $direction);
return Inertia::render($renderPage, [ return Inertia::render($renderPage, [
'filters' => $request->all('search', 'trashed'), 'filters' => $request->all('search', 'trashed', 'brand'),
'sort' => [ 'sort' => [
'by' => $sortBy, 'by' => $sortBy,
'direction' => $direction, 'direction' => $direction,
], ],
'cars' => $cars->filter($request->only('search', 'trashed')) 'cars' => $cars->filter($request->only('search', 'trashed', 'brand'))
->paginate(50) ->paginate(50)
->withQueryString() ->withQueryString()
->through(fn ($car) => [ ->through(fn ($car) => [

View File

@ -124,7 +124,7 @@ class Car extends Model
public function scopeSoldOnly($query) 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) public function scopeFilter($query, array $filters)
@ -150,6 +150,12 @@ class Car extends Model
} elseif ($trashed === 'only') { } elseif ($trashed === 'only') {
$query->onlyTrashed(); $query->onlyTrashed();
} }
})->when($filters['brand'] ?? null, function ($query, $brand) {
$query->whereHas('carModel', function($q) use ($brand)
{
$q->where('brand_id', '=', $brand);
})->get();
}); });
} }
} }