Ajoute les filtres

This commit is contained in:
Clement 2020-07-24 11:06:49 +02:00 committed by Gogs
parent a214c47cfd
commit 21bc5b2ab2
4 changed files with 63 additions and 12 deletions

View File

@ -32,4 +32,24 @@ class Home extends Model
{ {
return $query->where('price', '>=', $price); return $query->where('price', '>=', $price);
} }
public function scopeSurfaceMax(QueryBuilder $query, $price): QueryBuilder
{
return $query->where('surface', '<=', $price);
}
public function scopeSurfaceMin(QueryBuilder $query, $price): QueryBuilder
{
return $query->where('surface', '>=', $price);
}
public function scopeGardenSurfaceMax(QueryBuilder $query, $price): QueryBuilder
{
return $query->where('garden_surface', '<=', $price);
}
public function scopeGardenSurfaceMin(QueryBuilder $query, $price): QueryBuilder
{
return $query->where('garden_surface', '>=', $price);
}
} }

View File

@ -18,13 +18,18 @@ class HomeController extends Controller
public function index() public function index()
{ {
$homes = QueryBuilder::for(Home::class) $homes = QueryBuilder::for(Home::class)
// ->allowedFilters(['price', 'surface', 'garden_surface'])
->allowedFilters([ ->allowedFilters([
AllowedFilter::scope('price_min'), AllowedFilter::scope('price_min'),
AllowedFilter::scope('price_max'), AllowedFilter::scope('price_max'),
AllowedFilter::scope('surface_min'),
AllowedFilter::scope('surface_max'),
AllowedFilter::scope('garden_surface_min'),
AllowedFilter::scope('garden_surface_max'),
]) ])
->defaultSort('-created_at') // https://docs.spatie.be/laravel-query-builder/v2/features/sorting/#basic-usage ->defaultSort('-created_at') // https://docs.spatie.be/laravel-query-builder/v2/features/sorting/#basic-usage
->paginate(4) ->paginate(10)
->appends(request()->query()); ->appends(request()->query());
return view('homes.home', ['homes' => $homes]); return view('homes.home', ['homes' => $homes]);

View File

@ -3,6 +3,7 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@ -23,6 +24,9 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot() public function boot()
{ {
// Str::macro('currency', static function ($price)
{
return sprintf('%s €', number_format($price, 0, ',', ' '));
});
} }
} }

View File

@ -3,7 +3,7 @@
@section('content') @section('content')
<div class="container"> <div class="container">
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="col-md-8"> <div class="col-md-12">
<div class="card"> <div class="card">
<div class="card-header">{{ __('Dashboard') }}</div> <div class="card-header">{{ __('Dashboard') }}</div>
@ -15,20 +15,42 @@
@endif @endif
<div class="row"> <div class="row">
<div class="col-sm-12 mt-4"> <div class="col-sm-12 mt-4 mb-2">
<p>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#filters" aria-expanded="false" aria-controls="filters">
Filtrer
</button>
</p>
<div class="collapse" id="filters">
<div class="card card-body">
<form> <form>
<label for="price_min">Min prix:</label><input type="number" name="filter[price_min]" id="price_min"> <div class="row form-group">
<label for="price_max">Max prix:</label><input type="number" name="filter[price_max]" id="price_max"> <label class="col-lg-2" for="price_min">Min prix:</label><input type="number" name="filter[price_min]" id="price_min">
<label class="col-lg-2" for="price_max">Max prix:</label><input type="number" name="filter[price_max]" id="price_max">
</div>
<div class="row form-group">
<label class="col-lg-2" for="surface_min">Min surface:</label><input type="number" name="filter[surface_min]" id="surface_min">
<label class="col-lg-2" for="surface_max">Max surface:</label><input type="number" name="filter[surface_max]" id="surface_max">
</div>
<div class="row form-group">
<label class="col-lg-2" for="garden_surface_min">Min jardin:</label><input type="number" name="filter[garden_surface_min]" id="garden_surface_min">
<label class="col-lg-2" for="garden_surface_max">Max jardin:</label><input type="number" name="filter[garden_surface_max]" id="garden_surface_max">
</div>
<button class="btn btn-secondary" type="submit">Rechercher</button> <button class="btn btn-secondary" type="submit">Rechercher</button>
</form> </form>
</div> </div>
</div>
</div>
@if(count($homes) > 0) @if(count($homes) > 0)
@foreach($homes as $home) @foreach($homes as $home)
<div class="col-sm-6"> <div class="col-sm-4">
<div class="card"> <div class="card">
<img class="card-img-top" src="{{ asset($home->pictures[0]) }}"> <img class="card-img-top" src="{{ asset($home->pictures[0]) }}">
<div class="card-body"> <div class="card-body">
<h5 class="card-title">{{ $home->title }}</h5> <h5 class="card-title">
{{ $home->title }}
<small class="badge badge-@if($home->price <= 100000)primary @elseif($home->price <= 200000)success @else()warning @endif">{{ Str::currency($home->price) }}</small>
</h5>
<p class="card-text">{{ $home->excerpt() }}</p> <p class="card-text">{{ $home->excerpt() }}</p>
<a href="{{ route('home.show', ['id' => $home->id]) }}" class="btn btn-primary">Voir la maison</a> <a href="{{ route('home.show', ['id' => $home->id]) }}" class="btn btn-primary">Voir la maison</a>
</div> </div>