#How to Get All Playlists an Artist Appears On via Songs As An Eloquent Relationship?

22 messages · Page 1 of 1 (latest)

subtle vale
#

I have the following models:
Song
Artist
Playlist

Pivot tables:
song_artist artist_song
song_playlist playlist_song

Relationships:
A song belongs to many artists
A song belongs to many playlists

Goal
I want to create a relation manager in the Artist resource that lists all playlists containing at least one song by that artist.

Problem
I’m trying to define a relationship between the Artist and Playlist models, but I’m not sure how to connect them through the existing pivots—or if this is even possible.

Question
How can I define this relationship so I can access an artist’s playlists through their songs? I guess I need it as an eloquent relationship so i can properly setup the relation manager

nimble pond
#

From chatgpt:

class Artist extends Model
{
    public function playlists()
    {
        return $this->belongsToMany(
            Playlist::class,
            'song_playlist',
            'song_id',
            'playlist_id'
        )
        ->join('song_artist', 'song_artist.song_id', '=', 'song_playlist.song_id')
        ->where('song_artist.artist_id', $this->id)
        ->distinct();
    }

    public function songs()
    {
        return $this->belongsToMany(Song::class);
    }
}
#
{
    public function artists()
    {
        return $this->belongsToMany(
            Artist::class,
            'song_artist',
            'song_id',
            'artist_id'
        )
        ->join('song_playlist', 'song_playlist.song_id', '=', 'song_artist.song_id')
        ->distinct();
    }

    public function songs()
    {
        return $this->belongsToMany(Song::class);
    }
}```
subtle vale
#

im trying it lets see 🤔

nimble pond
#

Resource:

use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;

class PlaylistsRelationManager extends RelationManager
{
    protected static string $relationship = 'playlists';

    protected static ?string $title = 'Playlists';

    public function table(Tables\Table $table): Tables\Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->label('Playlist')
                    ->searchable()
                    ->sortable(),

                Tables\Columns\TextColumn::make('songs_count')
                    ->counts('songs')
                    ->label('Songs'),
            ])
            ->defaultSort('name')
            ->headerActions([
                // :x: Disable attach — this is a derived relationship
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
            ]);
    }
}```
subtle vale
# nimble pond From chatgpt: ``` class Artist extends Model { public function playlists() ...

ive modified my question, i had wrong pivot table names, i corrected it, and i used this playlists fn, like this:

public function playlists(): BelongsToMany
{
    return $this->belongsToMany(
        Playlist::class,
        'playlist_song',
        'song_id',
        'playlist_id'
    )
    ->join('artist_song', 'artist_song.song_id', '=', 'playlist_song.song_id')
    ->where('artist_song.artist_id', $this->id)
    ->distinct();
}

but i get:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'artist_song.artist_id'

when the column exists

#

also, i already tried to ask this to the AI before asking it here, when i get stuck is when i ask in this filament discord server

#

so i think AI has no clue on how to achieve it, or im prompting it wrong to it

#

but must be a wayyy hmmm i keep trying

nimble pond
#

What I have in mind is using hasManyThrough. But I don't know if filament relationship supports it

frank palm
subtle vale
subtle vale
frank palm
#

HasManyDeep class extends HasManyThrough which is supported by relation managers, I guess it is worth a try

subtle vale
#

trying it right now

subtle vale
#

but then i installed the package

#

and also works

#

so...

#

ill use the package lol haha

#

thing is, i already knew the package, but thought that was not compatible or something