PHP code example of lartisan / laravel-sortable-medialibrary

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

    

lartisan / laravel-sortable-medialibrary example snippets


'providers' => [
    ...
    Spatie\MediaLibrary\MediaLibraryServiceProvider::class,
    Spatie\EloquentSortable\EloquentSortableServiceProvider::class,
];

...
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;

class MyCustomMedia extends BaseMedia implements Sortable
{
    use SortableTrait;
    
    /**
     * Define the column used for sorting
     * @var array
     */
    public $sortable = [
        'order_column_name' => 'order_column',
        'sort_when_creating' => true,
    ];

    /**
     * @return Builder
     */
    public function buildSortQuery(): Builder
    {
        return static::query()
            ->where('collection_name', $this->collection_name);
    }
    ...
}

...
use Spatie\MediaLibrary\MediaCollections\Models\Concerns\IsSorted;
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;

class MyCustomMedia extends BaseMedia
{
    use IsSorted;
}
bash
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Spatie\EloquentSortable\EloquentSortableServiceProvider" --tag="config"