codekerala / laravel-and-vue.js-spa-Recipe-Box

Single Page Application with Laravel 5.4 and Vue.js 2 - Recipe Box
124 stars 53 forks source link

Delete all not updated against sync() #5

Open Siebov opened 6 years ago

Siebov commented 6 years ago

Hello. You are using

RecipeIngredient::whereNotIn('id',$ingredients_updated)
            ->where('recipe_id',$recipe->id)
            ->delete();

        RecipeDirection::whereNotIn('id',$directions_updated)
            ->where('recipe_id',$recipe->id)
            ->delete();

and why not just

$recipe->ingredients()->sync($ingredients_updated);
$recipe->directions()->sync($directions_updated);

insted?

anishdcruz commented 6 years ago

@Sebbba i didnt know about this. Are you sure this works? I will test this later

Siebov commented 6 years ago

@anishdcruz https://laravel.com/docs/5.5/eloquent-relationships Syncing Associations - chapter.