Question Body:
I’m encountering a peculiar issue in my Laravel API setup. I have a resource controller for managing student data, including deletion. When I make a DELETE request to http://127.0.0.1:8000/api/student/1
(for example, with id=1
), the first request successfully deletes the student and returns a JSON response with a success message:
{
"message": "Student deleted successfully"
}
However, if I make another DELETE request immediately afterward with the same URL and ID, instead of receiving a similar success message, I get a 404 Not Found error with a rendered HTML response.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Not Found</title>
<style>
/* code css here */
</style>
<link rel='stylesheet' type='text/css' property='stylesheet'
href='//127.0.0.1:8000/_debugbar/assets/stylesheets?v=1697098252&theme=auto' data-turbolinks-eval='false'
data-turbo-eval='false'>
<script src='//127.0.0.1:8000/_debugbar/assets/javascript?v=1697098252' data-turbolinks-eval='false'
data-turbo-eval='false'></script>
<script data-turbo-eval="false">
jQuery.noConflict(true);
</script>
<script>
//js code here
</script>
</head>
<body class="antialiased">
<div
class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center sm:pt-0">
<div class="max-w-xl mx-auto sm:px-6 lg:px-8">
<div class="flex items-center pt-8 sm:justify-start sm:pt-0">
<div class="px-4 text-lg text-gray-500 border-r border-gray-400 tracking-wider">
404 </div>
<div class="ml-4 text-lg text-gray-500 uppercase tracking-wider">
Not Found </div>
</div>
</div>
</div>
<script type="text/javascript">
//js script code
</script>
</body>
</html>
The code of the Controller function
// StudentController.php
<?php
use AppModelsStudent;
use AppHttpControllersController;
use IlluminateSupportFacadesValidator;
class StudentController extends Controller
{
// ...other code
public function destroy(Student $student)
{
try {
$student->delete();
$student->person()->delete();
$student->guardian1()->delete();
$student->guardian2()->delete();
return response()->json(['message' => 'Student deleted successfully'], 200);
} catch (IlluminateDatabaseEloquentModelNotFoundException $notFoundException) {
return response()->json(['error' => 'Student not found', 'exception' => $notFoundException->getMessage()], 404);
} catch (Exception $e) {
return response()->json(['error' => 'An error occurred while deleting the student', 'exception' => $e->getMessage()], 500);
}
}
}
the Code of the Api Route
// api.php
<?php
use IlluminateSupportFacadesRoute;
Route::group(['middleware' => 'auth:api'], function(){
// ...other code
Route::resource('student', AppHttpControllersStudentController::class);
});
Additional Information:
- This behavior occurs consistently whenever I attempt to delete a student for the second time.
- I’ve ensured that the appropriate routes are registered in
api.php
. - I’m using Laravel version 10.32.1
I expected that both DELETE requests would successfully return a JSON response with a message indicating successful deletion, or Error message of not exists element.
2
Answers
Make sure to alias the exception class you are checking for.
Without this you are checking for an instance of
AppExceptionsModelNotFoundException.
I understand you:
It’s Laravel implicit binding, this means when you call this function It’s doing behind the scenes something like
Student::find($student->id)
and this returns you error 404 because It’s already deleted so you can’t catch it here 🙂You can change it to
destroy($id)
and do more work manually or change it to custom model binding: https://laravel.com/docs/11.x/routing#customizing-the-resolution-logic