PHP code example of exileofaranei / laravel-list-ordering

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

    

exileofaranei / laravel-list-ordering example snippets


Schema::create('tasks', function (Blueprint $table) {
    $table->id();
    $table->foreignId('board_id');
    $table->string('column');
    $table->orderingRank(); // 64-char rank column, byte-comparable collation per driver
    $table->timestamps();

    $table->unique(['board_id', 'column', 'rank']);
});

use ExileOfAranei\ListOrdering\Concerns\HasOrdering;
use ExileOfAranei\ListOrdering\Contracts\Orderable;

class Task extends Model implements Orderable
{
    use HasOrdering;

    public function orderingGroupColumns(): array
    {
        return ['board_id', 'column'];
    }
}

class GalleryImage extends Model implements Orderable
{
    use HasOrdering;

    public function orderingGroupColumns(): array
    {
        return ['gallery_id'];
    }
}

class LandingFeature extends Model implements Orderable
{
    use HasOrdering;

    public function orderingGroupColumns(): array
    {
        return [];
    }
}

use ExileOfAranei\ListOrdering\Support\GroupKey;

// Insert a new task at the end of a board column.
$task->placeInto(GroupKey::of(['board_id' => 42, 'column' => 'todo']), $lastInColumn, null);

// Move it to a different column, between two existing tasks.
$task->placeInto(GroupKey::of(['board_id' => 42, 'column' => 'done']), $doneFirst, $doneSecond);

$task->placeAtEnd(GroupKey::of(['board_id' => 42, 'column' => 'todo']));
$task->placeAtStart(GroupKey::of(['board_id' => 42, 'column' => 'todo']));
$task->placeAfter($anchorTask);  // target group is $anchorTask's group right now
$task->placeBefore($anchorTask);

$clone->placeAtRank(GroupKey::of(['board_id' => 42, 'column' => 'todo']), $original->rank);

$clone = new Task(['board_id' => 99, 'column' => 'todo']); // $clone's own, different group
$clone->cloneRankFrom($original);

use ExileOfAranei\ListOrdering\Support\BatchReorderer;

// $newOrderOfKeys is every primary key currently in the group, in the order
// the UI now wants them — e.g. straight from a SortableJS `onEnd` payload.
BatchReorderer::apply(Task::class, GroupKey::of(['board_id' => 42, 'column' => 'todo']), $newOrderOfKeys);

Task::ordered(GroupKey::of(['board_id' => 42, 'column' => 'todo']))->get();

Task::ordered(GroupKey::of(['board_id' => 42, 'column' => 'todo']), direction: 'desc')->get();

use ExileOfAranei\ListOrdering\Testing\AssertsOrderingIndex;

class TaskOrderingTest extends TestCase
{
    use AssertsOrderingIndex;

    public function test_ordering_index_matches(): void
    {
        $this->assertOrderingIndexMatches(Task::class);
    }
}
bash
php artisan list-ordering:check-index "App\Models\Task"
php artisan list-ordering:check-index --all