features/filter/1 #7

Merged
Shikiryu merged 3 commits from features/filter/1 into main 2020-07-24 14:53:46 +02:00
7 changed files with 187 additions and 7 deletions

View File

@ -4,6 +4,7 @@ namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\QueryBuilder\QueryBuilder;
class Home extends Model
{
@ -21,4 +22,34 @@ class Home extends Model
{
return Str::words($this->description, 10);
}
public function scopePriceMax(QueryBuilder $query, $price): QueryBuilder
{
return $query->where('price', '<=', $price);
}
public function scopePriceMin(QueryBuilder $query, $price): QueryBuilder
{
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

@ -5,6 +5,8 @@ namespace App\Http\Controllers;
use App\Home;
use App\Parser;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
class HomeController extends Controller
{
@ -15,7 +17,21 @@ class HomeController extends Controller
*/
public function index()
{
$homes = Home::all();
$homes = QueryBuilder::for(Home::class)
->allowedFilters([
AllowedFilter::scope('price_min'),
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
->paginate(10)
->appends(request()->query());
return view('homes.home', ['homes' => $homes]);
}

View File

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

View File

@ -18,7 +18,8 @@
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "^7.0",
"laravel/tinker": "^2.0",
"laravel/ui": "^2.1"
"laravel/ui": "^2.1",
"spatie/laravel-query-builder": "^2.8"
},
"require-dev": {
"facade/ignition": "^2.0",

66
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "01ffe58a603d5640b76b6f85688ea1a8",
"content-hash": "71386ccf87ea9b8b3be2921f40baca69",
"packages": [
{
"name": "absmoca/leboncoin",
@ -2234,6 +2234,70 @@
],
"time": "2020-03-29T20:13:32+00:00"
},
{
"name": "spatie/laravel-query-builder",
"version": "2.8.2",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-query-builder.git",
"reference": "2737b2298e8bfeb632a80013646943307bf31775"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-query-builder/zipball/2737b2298e8bfeb632a80013646943307bf31775",
"reference": "2737b2298e8bfeb632a80013646943307bf31775",
"shasum": ""
},
"require": {
"illuminate/database": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0",
"illuminate/http": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0",
"illuminate/support": "~5.6.34|~5.7.0|~5.8.0|^6.0|^7.0",
"php": "^7.1"
},
"require-dev": {
"ext-json": "*",
"orchestra/testbench": "~3.6.0|~3.7.0|~3.8.0|^4.0|^5.0",
"phpunit/phpunit": "^7.0|^8.0|^9.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\QueryBuilder\\QueryBuilderServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Spatie\\QueryBuilder\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alex Vanderbist",
"email": "alex@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Easily build Eloquent queries from API requests",
"homepage": "https://github.com/spatie/laravel-query-builder",
"keywords": [
"laravel-query-builder",
"spatie"
],
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
}
],
"time": "2020-05-25T09:36:37+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v6.2.3",

32
config/query-builder.php Normal file
View File

@ -0,0 +1,32 @@
<?php
/**
* @see https://github.com/spatie/laravel-query-builder
*/
return [
/*
* By default the package will use the `include`, `filter`, `sort`
* and `fields` query parameters as described in the readme.
*
* You can customize these query string parameters here.
*/
'parameters' => [
'include' => 'include',
'filter' => 'filter',
'sort' => 'sort',
'fields' => 'fields',
'append' => 'append',
],
/*
* Related model counts are included using the relationship name suffixed with this string.
* For example: GET /users?include=postsCount
*/
'count_suffix' => 'Count',
];

View File

@ -3,7 +3,7 @@
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="col-md-12">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
@ -15,19 +15,51 @@
@endif
<div class="row">
<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>
<div class="row form-group">
<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>
</form>
</div>
</div>
</div>
@if(count($homes) > 0)
@foreach($homes as $home)
<div class="col-sm-6">
<div class="col-sm-4">
<div class="card">
<img class="card-img-top" src="{{ asset($home->pictures[0]) }}">
<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>
<a href="{{ route('home.show', ['id' => $home->id]) }}" class="btn btn-primary">Voir la maison</a>
</div>
</div>
</div>
@endforeach
<div class="col-sm-12 mt-4">
{{ $homes->links() }}
</div>
@else
Aucune maison enregistrée :(
@endif