承接 kiritokatklian/laravel-sort-request 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kiritokatklian/laravel-sort-request

最新稳定版本:2.0.0

Composer 安装命令:

composer require kiritokatklian/laravel-sort-request

包简介

Sorting logic for your requests, simplified.

README 文档

README

Sorting logic for your requests, simplified!

Laravel Sort Request PHP 7.4+ Laravel Packagist License

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:

PHP Version Sort Request Version
PHP 7.3 Sort Request 1.0
PHP 7.4 Sort Request 2.0
PHP 8.0 Sort Request 2.0

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

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.

统计信息

  • 总下载量: 341
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-17