Laravel Sort and Filter

This package allows you to sort, filter and even search Eloquent models using the Request object.

No need to write conditional code inside you controller to handle sort, filter or search parameters coming from front end through URL (for example from a Javascrpt application)

Just pass the request object and the sorting and filters should automatically work.

Installation

You can install the package via composer:

composer require amitavroy/laravel-sort-and-filter

You can publish the config file with:

php artisan vendor:publish --provider="AmitavSortAndFilterSortAndFilterServiceProvider" --tag="config"

Usage

This pacakge provides with a Trait which you need to use in any Model that you want to have the ability to sort, filter or search. For example, in the user model, you need to add

use SortAndFilter;

Once done, you can add the sort, filter or search function to the Model inside a query and pass the request object directly as show below.

You can sort and/or filter on any model after adding the trait as shown below:

User::query() ->sort($request) ->filter($request) ->get();

With this, you have the ability to pass paramters through URL like this:

http://localhost:8000?sortBy=name&sortOrder=desc http://localhost:8000?filterBy=name&filterValue=Amitav

If you want to control which fields can be filtered and which fields can be sorted, then you can create a protected field in your model with name $sortable to control sort fields. And, create a protected field with name $filterable to control which fields can be used to filter.

You can even search on any model as shown below:

User::query() ->search($request) ->get();

NOTE: The search is going to be a database query and the package runs a like query. And hence, be careful about the number of string that you allow in validation before you send the request object for search. Internally, the query will be something like:

SELECT * FROM user WHERE name LIKE "amit%";

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email reachme@amitavroy.com instead of using the issue tracker.

Credits

Amitav Roy All Contributors

License

The MIT License (MIT). Please see License File for more information.

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。