W
W
WebDev2019-06-10 13:19:30
Laravel
WebDev, 2019-06-10 13:19:30

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

1 answer(s)
P
pLavrenov, 2019-06-15
@kirill-93

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 question

Ask a Question

731 491 924 answers to any question