Skip to content

Commit 38dff01

Browse files
committed
Prevent duplicate shelf ingredients in batch store
1 parent 5c346e6 commit 38dff01

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

app/Http/Controllers/ShelfController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ public function batchStoreBarIngredients(ShelfIngredientsRequest $request, int $
262262
if ($request->user()->cannot('manageShelf', $bar)) {
263263
abort(403);
264264
}
265+
$bar->load('shelfIngredients');
265266

267+
$existingBarShelfIngredients = $bar->shelfIngredients->pluck('ingredient_id');
266268
$ingredients = DB::table('ingredients')
267269
->select('id')
268270
->where('bar_id', $bar->id)
@@ -271,6 +273,10 @@ public function batchStoreBarIngredients(ShelfIngredientsRequest $request, int $
271273

272274
$models = [];
273275
foreach ($ingredients as $dbIngredientId) {
276+
if ($existingBarShelfIngredients->contains($dbIngredientId)) {
277+
continue;
278+
}
279+
274280
$userIngredient = new BarIngredient();
275281
$userIngredient->ingredient_id = $dbIngredientId;
276282
$models[] = $userIngredient;

0 commit comments

Comments
 (0)