vendredi 30 novembre 2018

Array values set in pivot table along with index value laravel

I have three tables playlist, music and pivot table playlist_music, i want to add playlist and music in pivot table playlist_music. my array of music is music_ids : [1,2,3]. I have to store index also in pivot table, so my code is :

public function create(array $attributes = [])
{
$playlist = parent::create($attributes);
if (isset($attributes['music_ids'])) {
    $playlistMusic = $attributes['music_ids'];
    $music = [];
    foreach ($playlistMusic as  $index => $music ) {
        $tracks[$music['music_id']] = ['index' => $index];
       $tracks[$music['music_id']] = $music;
    }
    $playlist->music()->sync($music);
}
return $playlist;
}

how can i store individual values from array music_ids to the field music_id in pivot table. It is in belongsToMany relationship. please suggest me. Any help would be appreciated.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2U2tzSI
via IFTTT

1 commentaire: