PHP code example of thytanium / eloquent-positionable

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

    

thytanium / eloquent-positionable example snippets


use Illuminate\Database\Eloquent\Model;
use Thytanium\EloquentPositionable\Positionable;

class MyModel extends Model
{
    use Positionable;

    // optional parameters
    protected $positionable = [
        'column' => 'column_name', // column used for positioning
        'start' => 5, // starting position
    ];
}

$myModel = new MyModel();
$myModel->save();

$myModel->getPosition(); // by default will return 1, or the custom starting position

$model->moveTo(5); // moves model to position 5

$model->moveToStart();

$model->moveToEnd();

$model->moveUp(); // moves the model 1 place up
$model->moveUp(5); // moves the model 5 places up

$model->moveDown(); // moves the model 1 place down
$model->moveDown(5); // moves the model 5 places down

$model->moveStep(-1); // moves the model 1 place up
$model->moveStep(1); // moves the model 1 place down

Model::setNewOrder([1, 2, 3]); // sets sequencial order on models with ids 1, 2 and 3 starting from position 1
Model::setNewOrder([1, 2, 3], 5); // sets sequencial order on models with ids 1, 2 and 3 starting from position 5

MyModel::ordered()->get(); // get all models in ascending order
MyModel::ordered('desc')->get(); // get all models in descending order

MyModel::position(1)->first(); // get the model at the start

MyModel::positionBetween([1, 9])->get(); // get models between positions 1 and 9
MyModel::positionBetween([1, 9])->ordered()->get(); // get models in ordered fashion between positions 1 and 9

$model1->swapPositions($model2); // swap position with another model instance
$model1->swapPositions(2); // swap position with whichever model holds position 2

use Illuminate\Database\Eloquent\Model;
use Thytanium\EloquentPositionable\Positionable;

class MyModel extends Model
{
    use Positionable;

    // optional parameters
    protected $positionable = [
        'column' => 'column_name', // column used for positioning
        'start' => 5, // starting position
        'groups' = ['user_id'], // columns for grouping
    ];
}