PHP code example of almirhodzic / nova-sortable-5

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

    

almirhodzic / nova-sortable-5 example snippets


Schema::create('services', function (Blueprint $table) {
    $table->id();
    $table->unsignedInteger('sort_order')->default(0);
    // ... other columns
    $table->timestamps();
});

Schema::table('services', function (Blueprint $table) {
    $table->unsignedInteger('sort_order')->default(0)->after('id');
});

App\Models\Service::query()->each(function ($service, $index) {
    $service->update(['sort_order' => $index + 1]);
});

use AlmirHodzic\NovaSortable5\HasSortableRows;

class Service extends Model
{
    use HasSortableRows;

    protected string $sortableColumn = 'sort_order';
}

use AlmirHodzic\NovaSortable5\Sortable;
use AlmirHodzic\NovaSortable5\SortableResource;

class Service extends Resource
{
    use SortableResource;

    public function fields(NovaRequest $request): array
    {
        return [
            Sortable::make('Order', 'sort_order'),

            // ... other fields
        ];
    }
}

return [
    // Default database column for sorting
    'default_column' => 'sort_order',

    // Default sort direction: 'asc' or 'desc'
    'order_direction' => 'asc',

    // Show the drag handle (bars icon)
    'show_drag_handle' => true,

    // Show up/down arrow buttons
    'show_order_arrows' => true,

    // Show the order number
    'show_order_number' => true,

    // Show toast messages after reordering
    'show_toast' => true,

    // Authentication guards for the API routes
    'guards' => ['web'],
];

Sortable::make('Order', 'sort_order')
    ->autoAssignOnCreate(false)

Service::create([
    'name' => 'My Service',
    'sort_order' => 5, // must be set manually
]);

// Auto-assigns the next order value when creating a model
$service = Service::create(['name' => 'New Service']);
// sort_order is automatically set to max + 1

// Move a model to a specific position (re-indexes other rows)
$service->moveToPosition(1);

// Query scope for custom ordering
Service::orderBySortable('desc')->get();

// Get the sortable column name
$service->getSortableColumn(); // 'sort_order'
bash
php artisan vendor:publish --tag=frontbyte-nova-sortable-config
bash
php artisan migrate
bash
php artisan tinker