A
A
Artem00712018-07-11 10:50:24
Laravel
Artem0071, 2018-07-11 10:50:24

Is it possible to somehow remake firstOrFail?

Is it possible to somehow remake the firstOrFail error when using the API?
I created a controller that has this very method, and if not found, a huge standard error is simply thrown:

[{
    "message": "No query results for model [App\\Models\\User\\Confirmation].",
    "exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
    "file": "/Users/.../vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php",
    "line": 199,
    "trace": [
       ....

I want to change it to something like this:
{status: 404, errors: [{'Not found': 'Model was not found'}]}

I thought to do it through Exceptions/Handler, but then this error will also be issued in the normal (non-API) version.
In general, I don’t do logic in the controllers themselves, but create Requests and under authorizeand rulescreate an additional method, and just do it in the controller. I do $request->execute();
n’t know how correct this is, of course, but it’s more convenient for me than making huge controllers.
So, these requests do not inherit FormRequest, but I created an additional ApiFormRequestone, in which I just managed to redo the errors failedAuthorizationand failedValidation
I understand that I firstOrFailcan’t redo it right there, but maybe there is a place where you can do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Nagorny, 2018-07-11
@Artem0071

It is better to do through Exceptions/Handler.
Since you have debug mode enabled, you will get a full error, and in disabled mode - then a 404 page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question