diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php index 1e04fa1344..2e08472bb4 100644 --- a/app/Http/Controllers/Api/LocationsController.php +++ b/app/Http/Controllers/Api/LocationsController.php @@ -148,6 +148,13 @@ class LocationsController extends Controller { $this->authorize('update', Location::class); $location = Location::findOrFail($id); + + if ($request->input('parent_id') == $id) { + + return response()->json(Helper::formatStandardApiResponse('error', null, 'A location cannot be its own parent. Please select a different parent ID.')); + } + + $location->fill($request->all()); if ($location->save()) { diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php index b3df9efaff..bf8a279c2e 100755 --- a/app/Http/Controllers/LocationsController.php +++ b/app/Http/Controllers/LocationsController.php @@ -145,6 +145,11 @@ class LocationsController extends Controller return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist')); } + if ($request->input('parent_id') == $locationId) { + return redirect()->back()->withInput()->with('error', 'A location cannot be its own parent. Please select a different parent location.'); + } + + // Update the location data $location->name = $request->input('name'); $location->parent_id = $request->input('parent_id', null);