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; } }