Support the ongoing development of Laravel.io →
Eloquent
Last updated 1 month ago.
0

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();
Last updated 1 month ago.
0

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.

Last updated 1 month ago.
0

What about:

    $video->audiochannels()->delete();
    $video->audiochannels()->saveMany(// array of audiochannels);

Hope this helps

Last updated 1 month ago.
0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

thedamon thedamon thedamon Joined 2 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.