PHP code example of kiritokatklian / laravel-sort-request

1. Go to this page and download the library: Download kiritokatklian/laravel-sort-request library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kiritokatklian / laravel-sort-request example snippets


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'
        ];
    }
}

function getSortableColumns(): array
{
    return ['name', 'price'];
}

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...
    }
}