F
Filament12mo ago
ericmp

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 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')
)
;
}
}
This is what im getting just loading the view, without even interacting with it: I expected to be able to change the order successfully
No description
4 Replies
ericmp
ericmpOP12mo ago
later on, i tried this:
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'),
])
;
}
}
now i can see the view, but once i start reordering:
No description
ericmp
ericmpOP12mo ago
models:
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')
;
}
}
and yeah, the order column does exist in the playlist_song table
ericmp
ericmpOP12mo ago
3rd try:
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'),
])
;
}
}
result when reordering:
No description
ericmp
ericmpOP11mo ago
anyone?
Want results from more Discord servers?
Add your server