PHP code example of neurony / laravel-sort

1. Go to this page and download the library: Download neurony/laravel-sort 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/ */

    

neurony / laravel-sort example snippets




namespace App;

use Illuminate\Database\Eloquent\Model;
use Neurony\Sort\Traits\IsSortable;

class YourModel extends Model
{
    use IsSortable;
    
    ...
}



namespace App\Http\Controllers;

use App\YourModel;
use Illuminate\Http\Request;

class YourController extends Controller
{
    public function index(Request $request)
    {
        $records = YourModel::sorted($request->all())->get();
    }
}

// in our case, what's passed inside that parameter is this array:
['sort' => 'name', 'direction' => 'asc']



namespace App\Sorts;

use Neurony\Sort\Objects\Sort;

class YourSort extends Sort
{
    /**
     * Get the request field name to sort by.
     *
     * @return string
     */
    public function field()
    {
        return 'field-to-sort-by';
    }

    /**
     * Get the direction to sort by.
     *
     * @return string
     */
    public function direction()
    {
        return 'direction-to-sort-in';
    }
}



namespace App\Http\Controllers;

use App\YourModel;
use App\Sorts\YourSort;
use Illuminate\Http\Request;

class YourController extends Controller
{
    public function index(Request $request, YourSort $sort)
    {
        $records = YourModel::sorted($request->all(), $sort)->get();
    }
}