From 9da87b2b953b59eea68783c5f67982aece227243 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 1 Jul 2021 14:42:24 +0200 Subject: [PATCH] app/Jobs/ImportProfession.php: in getRecipe() sync the reagent's instead of only process them on recipe create. --- app/Jobs/ImportProfession.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/Jobs/ImportProfession.php b/app/Jobs/ImportProfession.php index bac514a..07a9250 100644 --- a/app/Jobs/ImportProfession.php +++ b/app/Jobs/ImportProfession.php @@ -129,19 +129,22 @@ class ImportProfession implements ShouldQueue 'item_id' => $item->id, 'category_id' => $category->id ]); - - // Reagents - foreach($data->items as $reagent) { - $item = Item::where('name', $reagent->name)->first(); - if (!$item) { - throw new Exception("Could not find item '{$reagent->name}'"); - } - $recipe->reagents()->attach($item, [ 'quantity' => $reagent->num ]); - } - - $recipe->push(); } + // Insert/Update Reagents + $reagents = []; + foreach($data->items as $reagent) { + + $item = Item::where('name', $reagent->name)->first(); + + if (!$item) { + throw new Exception("Could not find item '{$reagent->name}'"); + } + $reagents[$item->id] = [ 'quantity' => $reagent->num ]; + } + + $recipe->reagents()->sync($reagents); + return $recipe; } }