Answer the question
In order to leave comments, you need to log in
How to throw errors with text in Laravel?
Laravel has an abort method where you can pass an error code and text as the second parameter.
I have a SPA application that accesses the backend via an API.
If I write the following code on the server:
How can I view this error from the client? An Ajax request is sent to the server, the response code is 404, but in the chrome preview, instead of the error text, I see the standard Laravel page (/resources/views/errors/404.blade.php) abort(404, 'Custom error message');
Answer the question
In order to leave comments, you need to log in
abortion just gives http - 404. and you can use json response which will return status 404.
public function index(Request $request) {
if($request->ajax()){
return response()->json(['message' => 'Custom error message.'], 404);
}
abort(404, 'Custom error message');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question