|Classes of Edward Paul||>||Laravel Database Filter Result Class||>||README.md||>||Download|
Laravel Database Filter
Need to filter database results with a query string? Filter Laravel Model With Query Strings
You can install the package via composer:
The package will automatically register itself, but if your laravel versions is < 5.5 you will need to add Infinitypaul\LaravelDatabaseFilter\LaravelDatabaseFilterServiceProvider::class, service provider under your config/app.php file.
Once the package is installed, an artisan command is available to you.
Generate a new model filter
This package will generate a new PHP file under app/Filters/ folder with the name SchoolFilter.php which will look like below.
Add The filterTrait and Register The SchoolFilter On Your Laravel Model Where The Filter Will Be Used.
Let assume we are to filter our database by checking those who have paid. Then we need to create a new filter class by using the artisan command below.
The above command will also generate a new PHP file under app/Filters folder with the name PaidFilter.php which will look like below.
Then we can register our filter conditions in the SchoolFilter We Created Above.
The $filters Array Key will be shown in the query string as paid.
The value PaidFilter::class takes us to the new class we created with the artisan command.
The filter method takes in our conditions or whatever checks against the database
To be strict about query params input, we can use the mapping method or leave empty for free entry
With the above setup, You can only accept free or paid in your query params, and their values return accordingly.
Lastly, You should be able to add the filter() passing in your $request in your controller.
You Can Register As Many Filter You Want
With The Above Settings, Your Url Will End Up Like This
You can add custom filter to your controller, by passing an array of filter into the filter scope
Bug & Features
If you have spotted any bugs, or would like to request additional features from the library, please file an issue via the Issue Tracker on the project's Github page: https://github.com/infinitypaul/laravel-database-filter/issues.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email firstname.lastname@example.org instead of using the issue tracker.
How can I thank you?
Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word!
Don't forget to follow me on twitter!
Thanks! Edward Paul.
The MIT License (MIT). Please see License File for more information.