PHP code example of oddvalue / eloquent-sortable

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

    

oddvalue / eloquent-sortable example snippets


class MyModel extends Model implements \Oddvalue\EloquentSortable\Sortable
{
    use \Oddvalue\EloquentSortable\SortableTrait;
}

# New model automatically sorted to the end of the list upon creation
$model = MyModel::create(['title' => 'foo']);

# Move the model before the 5th item in the list
$model->moveBefore(MyModel::where('order_column', 5)->first());
// OR
$model->moveBefore(5);

# Move the model after the 5th item in the list
$model->moveAfter(MyModel::where('order_column', 5)->first());
// OR
$model->moveAfter(5);

# Move the model between the 5th and 6th item in the list
$model->moveBetween(
    MyModel::where('order_column', 5)->first(), 
    MyModel::where('order_column', 6)->first()
);
// OR
$model->moveBetween(5, 6);
# This is useful when using a javacript library that provides the node before
# and after the location an item is dropped

# Move the model to the 5th item in the list
$model->moveTo(MyModel::where('order_column', 5)->first());
// OR
$model->moveTo(5);