Reorder table records from a pivot relationship
A playlist belongs to many songs.
A song belongs to many playlists.
The pivot between them has an attribute called
This is what im getting just loading the view, without even interacting with it:
I expected to be able to change the order successfully
order
(playlist_song.order
).
I just added a new resource into my PlaylistResource
-> SongsRelationManager
I want the user to be able to see the songs that a playlist has. also, i want them to be able to change the playlist_song.order
field
This is what i tried:
class SongsRelationManager extends RelationManager
{
protected static string $relationship = 'songs';
public function form(Form $form): Form
{
return SongResource::form($form);
}
public function table(Table $table): Table
{
return SongResource::table($table)
->reorderable('order')
->query(
Song::query()
->whereHas('playlists', function ($q) {
$q->where("playlists.id", $this->ownerRecord->id);
})
->orderBy('order')
)
;
}
}
class SongsRelationManager extends RelationManager
{
protected static string $relationship = 'songs';
public function form(Form $form): Form
{
return SongResource::form($form);
}
public function table(Table $table): Table
{
return SongResource::table($table)
->reorderable('order')
->query(
Song::query()
->whereHas('playlists', function ($q) {
$q->where("playlists.id", $this->ownerRecord->id);
})
->orderBy('order')
)
;
}
}
4 Replies
later on, i tried this:
now i can see the view, but once i start reordering:
class SongsRelationManager extends RelationManager
{
protected static string $relationship = 'songs';
public function form(Form $form): Form
{
return SongResource::form($form);
}
public function table(Table $table): Table
{
return $table
->reorderable('order')
->query(
PlaylistSong::query()
->where('playlist_id', $this->ownerRecord->id)
->orderBy('playlist_song.order')
)
->columns([
Tables\Columns\TextColumn::make('order'),
Tables\Columns\TextColumn::make('song.name'),
])
;
}
}
class SongsRelationManager extends RelationManager
{
protected static string $relationship = 'songs';
public function form(Form $form): Form
{
return SongResource::form($form);
}
public function table(Table $table): Table
{
return $table
->reorderable('order')
->query(
PlaylistSong::query()
->where('playlist_id', $this->ownerRecord->id)
->orderBy('playlist_song.order')
)
->columns([
Tables\Columns\TextColumn::make('order'),
Tables\Columns\TextColumn::make('song.name'),
])
;
}
}
models:
and yeah, the
class PlaylistSong extends Pivot
{
protected $guarded = ['id'];
protected $casts = [
'order' => 'integer',
];
public function song(): BelongsTo
{
return $this->belongsTo(Song::class);
}
public function playlist(): BelongsTo
{
return $this->belongsTo(Playlist::class);
}
}
class PlaylistSong extends Pivot
{
protected $guarded = ['id'];
protected $casts = [
'order' => 'integer',
];
public function song(): BelongsTo
{
return $this->belongsTo(Song::class);
}
public function playlist(): BelongsTo
{
return $this->belongsTo(Playlist::class);
}
}
class Song
{
public function playlists(): BelongsToMany
{
return $this->belongsToMany(Playlist::class)
->withTimestamps()
->withPivot('order')
->orderByPivot('order')
;
}
}
class Song
{
public function playlists(): BelongsToMany
{
return $this->belongsToMany(Playlist::class)
->withTimestamps()
->withPivot('order')
->orderByPivot('order')
;
}
}
class Playlist
{
public function songs(): BelongsToMany
{
return $this->belongsToMany(Song::class)
->withTimestamps()
->withPivot('order')
->orderByPivot('order')
;
}
}
class Playlist
{
public function songs(): BelongsToMany
{
return $this->belongsToMany(Song::class)
->withTimestamps()
->withPivot('order')
->orderByPivot('order')
;
}
}
order
column does exist in the playlist_song
table3rd try:
result when reordering:
class SongsRelationManager extends RelationManager
{
protected static string $relationship = 'songs';
public function form(Form $form): Form
{
return SongResource::form($form);
}
public function table(Table $table): Table
{
return $table
->reorderable('order')
->query($this->ownerRecord->songs()->getQuery())
->columns([
Tables\Columns\TextColumn::make('order'),
Tables\Columns\TextColumn::make('id'),
Tables\Columns\TextColumn::make('name'),
])
;
}
}
class SongsRelationManager extends RelationManager
{
protected static string $relationship = 'songs';
public function form(Form $form): Form
{
return SongResource::form($form);
}
public function table(Table $table): Table
{
return $table
->reorderable('order')
->query($this->ownerRecord->songs()->getQuery())
->columns([
Tables\Columns\TextColumn::make('order'),
Tables\Columns\TextColumn::make('id'),
Tables\Columns\TextColumn::make('name'),
])
;
}
}
anyone?