php - Many to Many Eloquent Relation -
i trying create many many relationship using laravel 4.1 , eloquent model. have 3 tables - songs, artists , song_artist:
---- songs -- id -- title -- genre ---- artists - id - name ---- song_artist - id - song_id - artist_id
since each song can have multiple artists , each artist can have multiple songs, assumed need use morphtomany()
method instead of hasmany()
or hasmanythrough()
methods.
how set relation access artists related song $song->artists()
, access songs related aritst using $artist->songs()
?
i've looked other questions on such this one, this one , this one.
any appreciated.
rename song_artist
artist_song
, use belongstomany
.
if don't want rename table, set manually:
// in artist return $this->belongstomany('song', 'song_artist'); // in song return $this->belongstomany('artist', 'song_artist');
Comments
Post a Comment