PHP code example of hippieua / laravel-sortablev2

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

    

hippieua / laravel-sortablev2 example snippets


public function up(): void
{
    Schema::table('categories', function (Blueprint $table) {
        $table->unsignedBigInteger('order_id')->default(0)->after('id');
    });

    Category::orderBy('id')->each(fn($category, $index) => $category->update(['order_id' => $index++]));
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Hippieua\Sortable2\Sortable2;

class Chapter extends Model
{
    use Sortable2;
}

protected function getSortableField(): string
{
    return 'custom_order_field';  // Default is 'order_id'
}

protected function getSortableRelation(): ?BelongsTo
{
    return $this->belongsTo(ParentModel::class);  // Default is null
}

$chapter = Chapter::find(1);
$chapter->moveUp();

$chapter = Chapter::find(1);
$chapter->moveDown();