I have three models
- Playlists
- Tracks
- PlaylistTracks
Playlists
$this->hasManyToMany(
"id",
"PlaylistTracks",
"playlist_id", "track_id",
"Tracks",
"id"
);
Tracks
$this->hasMany("id", "PlaylistTracks", "track_id");
PlaylistTracks
$this->belongsTo("track_id", "Tracks", "id");
$this->belongsTo("playlist_id", "Playlists", "id");
The playlistTracks table has and extra attribute called likes. Is there a way of setting this attribute without explicitly creating a new PlaylistTracks model.
This is what I have for creating a playlist with some tracks.
$playlist = new Playlists();
$playlist->setTitle('Test Playlist');
$tracks = array();
for($i = 1; $i < 10; $i++){
$track = new Tracks();
$track->setTitle('Track'. $i);
//I whish I could just call this
//$track->setLikes(10);
$tracks[] = $track;
}
$playlist->tracks = $tracks;
$playlist->save();