Answer the question
In order to leave comments, you need to log in
Why does the Laravel API respond with a 404 error under some circumstances?
Hello. I am creating my own API for one service. For testing I use Insomnia Rest.
It happens that the response to the API, the user, should receive an error code, but instead a 404 error is issued.
For example, if the user is not found:
UserController :
<?php
namespace App\Http\Controllers\Users;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class UsersController extends Controller{
public function show($id){
return User::findOrFail($id);
}
}
Route::group(['middleware' => 'auth:api'], function(){
Route::resource('users', 'Users\UsersController');
});
Answer the question
In order to leave comments, you need to log in
https://laravel.demiart.ru/laravel-api-404-answer-v...
the answer is briefly and clearly described here
I did it like this
public function render($request, $e)
{
if ($e instanceof ModelNotFoundException) {
return response()->json([
'success' => false,
'errors' => ['Record not found'],
'data' => [],
], 404);
}
return;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question