PHP code example of pion / laravel-eloquent-position

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

    

pion / laravel-eloquent-position example snippets


// ASC
YourModel::sorted()->get()

// DESC
YourModel::sortedByDESC()->get()

public function up()
    {
    Schema::table('pages', function (Blueprint $table) {
        $table->smallInteger('position')->default(0)->after('id');
    });

    // Update the order pages
    Artisan::call('model:position', [
        'model'=> \App\Models\Page\Page::class
    ]);
}

class Page extends Model
{
    use PositionTrait;

    public $table = 'pages';
    public $positionGroup = ['parent_slug'];

    protected $fillable = [
        'title', 'slug', 'parent_slug', 'content', 'description', 'position'
    ];
    
}

....

class YourModel extends Model {
    use PositionTrait, PositionEventsTrait;
    ....
}

YourModel::positioning(function($model, $query) {
    $query->query()->where('type', 'type'); // or etc
    \Log::info('positioning', 'To '.$model->getPosition().' from '.$query->oldPosition());
});

YourModel::positioned(function($model) {
    /// TODO
});
bash
php artisan model:position App\\Models\\YourModel