musa11971/laravel-sort-request

TBA

This package's canonical repository appears to be gone and the package has been frozen as a result.

1.0.1 2020-02-14 13:00 UTC

This package is auto-updated.

Last update: 2022-12-15 05:13:53 UTC


README

logo.png

Latest version on packagist GitHub Tests Action Status Quality score Total downloads

because you've got better things to do

Sorting logic for your requests, but simplified

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 install the package via composer:

composer require musa11971/laravel-sort-request

Usage

Basic sorting

Add the SortsViaRequest trait to your Laravel form request.

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:

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, like the following examples:

# 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 see CONTRIBUTING for details.

Security

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

Credits

Credits go to musa11971 for creating and maintaining the package.

Special thanks

Support me

I am a full-time software engineering student and work on this package in my free time. If you find the package useful, please consider making a donation! Every little bit helps. 💜

License

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