kiritokatklian / laravel-sort-request
Sorting logic for your requests, simplified.
Fund package maintenance!
Paypal
Requires
- php: ^7.4|^8.0
Requires (Dev)
- calebporzio/sushi: ^2.0
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
- spatie/phpunit-snapshot-assertions: ^4.2
- symfony/var-dumper: ^5.2
This package is auto-updated.
Last update: 2024-11-18 03:23:19 UTC
README
Sorting logic for your requests, simplified!
Laravel Sort Request
This Laravel package makes it easier to implement sorting logic into your app.
Consider the following examples:
# Get the cheapest items https://example.test/items?sort=price(asc) # Get the items sorted by name and size https://example.test/items?sort=name(asc),size(desc) # Get the most popular TV Shows (custom sorting behavior) https://example.test/tv-shows?sort=popularity(most-popular)
Installation
You can download a release and manually include it in your project:
Alternatively you can install the package via composer:
composer require kiritokatklian/laravel-sort-request
Usage
Basic sorting
Add the SortsViaRequest
trait to your Laravel form request.
use kiritokatklian\SortRequest\Tests\Support\Requests\FormRequest; use kiritokatklian\SortRequest\Traits\SortsViaRequest; class GetItemsRequest extends FormRequest { use SortsViaRequest; /** * Get the rules that the request enforces. * * @return array */ function rules() { return array_merge([ // This is where your normal validation rules go ], $this->sortingRules()); } /** * Returns the columns that can be sorted on. * * @return array */ function getSortableColumns(): array { return [ 'id', 'stackSize', 'displayName' ]; } }
As shown above, you will also need to implement the getSortableColumns
method into your form request. It should return an array of column names that can be sorted on.
So if you only wanted to allow sorting on the "name" and "price" columns, you would do:
function getSortableColumns(): array { return ['name', 'price']; }
Next, go to your controller and add the sortViaRequest
method as follows:
use Illuminate\Routing\Controller; use kiritokatklian\SortRequest\Tests\Support\Models\Item; use kiritokatklian\SortRequest\Tests\Support\Requests\GetItemsRequest; class ItemController extends Controller { /** * Returns a list of all items as JSON. * * @param GetItemsRequest $request * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection */ function get(GetItemsRequest $request) { $items = Item::sortViaRequest($request)->get(); // Do something with your models... } }
😎 That's all. You can now sort the models with the "sort" parameter.
# Sort a single column https://example.test/items?sort=price(asc) # Sort multiple columns https://example.test/items?sort=price(asc),name(desc),experience(asc)
Custom sorting
This package also allows you to have custom sorting behavior:
# Get the worst ranking users https://example.test/user?sort=ranking(worst) # Get the most delicious pastries, and sort them by cheapest https://example.test/pastries?sort=taste(most-delicious),price(cheapest)
Please refer the custom sorting docs for a guide on how to use this.
Testing
composer test
Contributing
Please refer to the Contributing guide to learn how you can help.
Credits
Credits go to kurozora for creating and maintaining the package.
Special thanks
- .. to Spatie for their template.
- .. to all contributors for contributing to the project.
Security
If you happen to find a security vulnerability, we would appreciate you letting us know at kurozoraapp@gmail.com and allowing us to respond before disclosing the issue publicly.
Getting in Touch
If you have any questions or just want to say hi, join the Kurozora Discord and drop a message on the #development channel.
License
Laravel Sort Request is an Open Source project covered by the MIT.