🚧 Commence les 1er filtres avec la pagination

This commit is contained in:
Clement 2020-07-23 17:21:26 +02:00
parent 49248fb16b
commit a8d9309103
6 changed files with 132 additions and 3 deletions

View File

@ -4,6 +4,7 @@ namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Spatie\QueryBuilder\QueryBuilder;
class Home extends Model class Home extends Model
{ {
@ -21,4 +22,14 @@ class Home extends Model
{ {
return Str::words($this->description, 10); 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);
}
} }

View File

@ -5,6 +5,8 @@ namespace App\Http\Controllers;
use App\Home; use App\Home;
use App\Parser; use App\Parser;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
class HomeController extends Controller class HomeController extends Controller
{ {
@ -15,7 +17,16 @@ class HomeController extends Controller
*/ */
public function index() public function index()
{ {
$homes = Home::all(); $homes = QueryBuilder::for(Home::class)
// ->allowedFilters(['price', 'surface', 'garden_surface'])
->allowedFilters([
AllowedFilter::scope('price_min'),
AllowedFilter::scope('price_max'),
])
->defaultSort('-created_at') // https://docs.spatie.be/laravel-query-builder/v2/features/sorting/#basic-usage
->paginate(4)
->appends(request()->query());
return view('homes.home', ['homes' => $homes]); return view('homes.home', ['homes' => $homes]);
} }

View File

@ -18,7 +18,8 @@
"guzzlehttp/guzzle": "^6.3", "guzzlehttp/guzzle": "^6.3",
"laravel/framework": "^7.0", "laravel/framework": "^7.0",
"laravel/tinker": "^2.0", "laravel/tinker": "^2.0",
"laravel/ui": "^2.1" "laravel/ui": "^2.1",
"spatie/laravel-query-builder": "^2.8"
}, },
"require-dev": { "require-dev": {
"facade/ignition": "^2.0", "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", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "01ffe58a603d5640b76b6f85688ea1a8", "content-hash": "71386ccf87ea9b8b3be2921f40baca69",
"packages": [ "packages": [
{ {
"name": "absmoca/leboncoin", "name": "absmoca/leboncoin",
@ -2234,6 +2234,70 @@
], ],
"time": "2020-03-29T20:13:32+00:00" "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", "name": "swiftmailer/swiftmailer",
"version": "v6.2.3", "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

@ -15,6 +15,13 @@
@endif @endif
<div class="row"> <div class="row">
<div class="col-sm-12 mt-4">
<form>
<label for="price_min">Min prix:</label><input type="number" name="filter[price_min]" id="price_min">
<label for="price_max">Max prix:</label><input type="number" name="filter[price_max]" id="price_max">
<button class="btn btn-secondary" type="submit">Rechercher</button>
</form>
</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-6">
@ -28,6 +35,9 @@
</div> </div>
</div> </div>
@endforeach @endforeach
<div class="col-sm-12 mt-4">
{{ $homes->links() }}
</div>
@else @else
Aucune maison enregistrée :( Aucune maison enregistrée :(
@endif @endif