leandreaci / filterable
Filter package to Laravel/Lumen
Installs: 428
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Type:package
Requires
- php: ^7.3|^8.0
- illuminate/database: >8.0
- illuminate/http: >8.0
Requires (Dev)
- phpunit/phpunit: ^9.5.10
This package is auto-updated.
Last update: 2024-10-29 01:09:55 UTC
README
Filter model by query string
Example: domain.com/route?id=1
Install package
composer require leandreaci/filterable *@dev
Using
Create a file in your Laravel/Lumen project
- app/Filters/ExampleFilter.php
<?php namespace App; use Carbon\Carbon; use Leandreaci\Filterable\QueryFilter; class ExampleFilter extends QueryFilter { public function id($id) { return $this->builder->where('id', $id); } public function start($date) { try{ $formattedDate = Carbon::createFromFormat('Y-m-d', $date)->startOfDay()->toDateTimeString(); return $this->builder->where('created_at','>', $formattedDate); }catch (\Exception $exception) { return $this->builder; } } }
- Use the Trait Filterable to you Model want to Filter
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Leandreaci\Filterable\Filterable; class ExampleModel extends Model { use Filterable; } ?>
- In Controller
<?php namespace App\Http\Controllers; use App\ExampleModel; class TransactionController extends Controller { /** * Display a listing of the resource. * * @param ExampleFilter $filter * @return TransactionsCollection */ public function index(ExampleFilter $filter) { return ExampleModel::filter($filter) ->paginate(10); } } ?>