NB! Sorting is impossible when your pivot table has multiple items with the same ID pairs.
Add two new columns to the pivot table: id
(primary key with auto increment) and sort_order
(integer).
# Can be created using Laravel's helper command
php artisan make:model -p ArtistTrack
Apply the interface, trait and also overwrite the buildSortQuery()
method to narrow the sorting scope.
<?php
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
class ArtistTrack extends Pivot implements Sortable
{
use SortableTrait;
public $primaryKey = 'id';
public $incrementing = true;
public $timestamps = false;
public $sortable = [
'order_column_name' => 'sort_order',
'sort_when_creating' => true,
];
public function buildSortQuery()
{
return static::query()
->where('track_id', $this->track_id); // As we're sorting Artists belonging to a Track, we're setting this to filter using track_id
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Artist extends Model
{
public function tracks()
{
return $this->belongsToMany(Track::class)
->using(ArtistTrack::class);
}
}
// Same applies to Track model
public function artists()
{
return $this->belongsToMany(Artist::class)
->using(ArtistTrack::class);
}
Add the HasSortableManyToManyRows
trait to the Resource you want to sort on BelongsTo (in this example, can be either Artist
or Track
), but let's go for Artist
.
use Outl1ne\NovaSortable\Traits\HasSortableManyToManyRows;
class Artist extends Resource
{
use HasSortableManyToManyRows;
// ...
}
Allow sorting only on a specific resource:
public $sortable = [
// Allow:
'only_sort_on' => \App\Nova\Resources\Chapter::class,
// Deny:
'dont_sort_on' => [
\App\Nova\Resources\Comic::class,
]
];
See this issue for guidance: nova-sortable/issues#60.