PHP code example of jedrzej / sortable

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

    

jedrzej / sortable example snippets


use Jedrzej\Sortable\SortableTrait;

class Post extends Eloquent
{
    use SortableTrait;

    // either a property holding a list of sortable fields...
    public $sortable = ['title', 'forum_id', 'created_at'];

    // ...or a method that returns a list of sortable fields
    public function getSortableAttributes()
    {
        return ['title', 'forum_id', 'created_at'];
    }
}

public $sortable = ['*'];

// return all posts sorted by creation date in descending order
Post::sorted('created_at,desc')->get();

// return all users sorted by level in ascending order and then by points indescending orders
User::sorted(['level,asc', 'points,desc'])->get();
   
// return all posts sorted by creation date in descending order by appending to URL
?sort=created_at,desc
//and then calling
Post::sorted()->get();

// return all users sorted by level in ascending order and then by points indescending orders by appending to URL
?sort[]=level,asc&sort[]=points,desc
// and then calling
User::sorted()->get();

// return all posts sorted by creation date in descending order
Post::sorted('created_at,desc')->get();

// in model class overwrite the sorting logic so that 'created' field is used instead of 'created_at'
public function sortCreatedAt($query, $direction = 'desc')
{
    return $query->orderBy('created', $direction);
}

// sort by latest first
protected $defaultSortCriteria = ['created_at,desc'];

// sort in desc order by default if no order is specified in request
protected $defaultSortOrder = 'desc';
// sort in asc order by default if no order is specified in request
protected $defaultSortOrder = 'asc';

 protected $sortParameterName = 'sortBy';