Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years 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 2 years 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 2 years ago.
0

What about:

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

Hope this helps

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

thedamon thedamon Joined 2 Feb 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.