1. Go to this page and download the library: Download app-ark/eloquent-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/ */
app-ark / eloquent-sortable example snippets
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
class MyModel extends Eloquent implements Sortable
{
use SortableTrait;
public $sortable = [
'order_column_name' => 'order_column',
'sort_when_creating' => true,
];
...
}
$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 1
$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 2
$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 3
//the trait also provides the ordered query scope
$orderedRecords = MyModel::ordered()->get();
/**
* the record for model id 3 will have record_column value 1
* the record for model id 1 will have record_column value 2
* the record for model id 2 will have record_column value 3
*/
MyModel::setNewOrder([3,1,2]);
/**
* the record for model id 3 will have record_column value 11
* the record for model id 1 will have record_column value 12
* the record for model id 2 will have record_column value 13
*/
MyModel::setNewOrder([3,1,2], 10);