I don't think there's an existing sync function but it wouldn't be too terribly difficult to create one, probably a worthy addition to a hasMany relation.
However, you could simplify your code as-is using something like this...
$audiochannel_ids = array();
foreach ($video->audiochannels as $key => $audiochannel)
{
$audiochannel->save();
$audiochannel_ids[] = $audiochannel->id;
}
AudioChannels::where('video_id', $video->id)->whereNotIn('id', $audiochannel_ids)->delete();
Looking for the same. BUT, instead of storing new records, and afterwards erasing those which are not new, I would prefer to erase all relevant data, and then store new records.
What about:
$video->audiochannels()->delete();
$video->audiochannels()->saveMany(// array of audiochannels);
Hope this helps
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community