Download the PHP package infinitypaul/laravel-database-filter without Composer
On this page you can find all versions of the php package infinitypaul/laravel-database-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download infinitypaul/laravel-database-filter
More information about infinitypaul/laravel-database-filter
Files in infinitypaul/laravel-database-filter
Package laravel-database-filter
Short Description Need to filter database results with a query string? Here's a beautiful, easy to extend laravel package to keep your code super tidy.
License MIT
Homepage https://github.com/infinitypaul/laravel-database-filter
Informations about the package laravel-database-filter
Laravel Database Filter
Ever been stuck with filtering a database table with lots of GET parameters? I can only imagine how bulky your code will be. Relax, the easiest solution to this problem is right here. Let's say we have to query our records table with the following get parameters
Installation
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.
Usage
Once the package is installed, an artisan command is available to you.
We would be working with the records table below:
Id | Department | Level | Score | Grade |
---|---|---|---|---|
1 | csc | 200 | 68 | b |
2 | physics | 100 | 90 | a |
3 | csc | 100 | 90 | a |
4 | physics | 200 | 60 | b |
5 | csc | 200 | 80 | a |
The Filtering be done in 6 simple steps. Perfect right! Let's begin:
* Step 1
Create a mother filter class, this is where all filters will be recorded. This can be created with this one line of code:
This package will generate a new PHP file RecordFilter.php under app/Filters folder. This is where all other filters will be created.. It wil look like this
* Step 2:
Since we are working with the records table, we will be working on the Record model. So open your Record model and include the following:
- The Filter Trait
- The Record Filter Class
* Step 3:
We would be filtering with 3 parameters (dept, level, grade):
Let's create filter classes for each filter
The above commands will also generate a new PHP file under app/Filters folder with the name DebtFilter.php, LevelFilter.php and GradeFilter.php which will look like below.
Well Done!!! Let's move to the next step
* Step 4:
Now we write our logic in each class:
In our DeptFilter class, we write the following:
In our LevelFilter class, we write the following:
In our GradeFilter class, we write the following:
The filter method takes in our conditions or whatever checks against the database
Bravo on completing that. Moving on!!!
* Step 5:
We write our search parameters against the filter class. Let's open up our mother filter class - RecordFilter to register all the conditions we have generated.
The $Filter array key is will be the query params.
* Step 6:
Assuming we have a controller RecordController, we can create a function to get our record:
We are all done!!!
When we enter the following into our browser
We will get the following result:
Tweak Time
To be strict about query params input, we can use the mapping method or leave empty for free entry.
Let's say when returning the data, we want all department with csc to return as CSC. We return values to the mappings function in the DeptFilter class as shown below:
With the above setup, once csc is entered in your query params, it will return CSC as value.
Lastly, You can add local scoped filter() by passing an array of filter into the filter scope.
You can also override your filter property from your controller in this manner
So instead of your filter to use the RecordFilter it will make use of PaulFilter
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.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] 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.
License
The MIT License (MIT). Please see License File for more information.