diff --git a/src/App/Http/Controllers/SelectionValueController.php b/src/App/Http/Controllers/SelectionValueController.php index 4693616..7a5c7b7 100644 --- a/src/App/Http/Controllers/SelectionValueController.php +++ b/src/App/Http/Controllers/SelectionValueController.php @@ -40,7 +40,23 @@ public function index(): JsonResponse */ public function store(SelectionValueRequest $request): JsonResponse { - $selectionValue = $this->selectionValue::query()->create($request->validated()); + if (method_exists($this->selectionValue, 'bootSoftDeletes')) { + // check for deleted values + $selectionValue = $this->selectionValue::withTrashed() + ->where('selection_type_id', $request->get('selection_type_id')) + ->where('value', $request->get('value')) + ->first(); + + if ($selectionValue->trashed()) { + // restore + $selectionValue->restoreQuietly(); + $selectionValue->update($request->validated()); + } else { + throw new Exception('Selection value already exists.', 400); + } + } else { + $selectionValue = $this->selectionValue::query()->create($request->validated()); + } return response()->json($selectionValue->refresh()); }