Laravel API: Override 'Model Not Found' with Exception

preview_player
Показать описание
Another free lesson from my latest course about Exceptions in Laravel.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

which laravel version is supported by this way? I'm making api with laravel 6 and cannot use renderable() method when I use route model binding in my controller.

ramadhnyg
Автор

What about the route model bindings i won't use try catch for that ?

QorashyTech
Автор

hi can you recommend some books to learn php and also laravel for beginners

philip
Автор

Yeah, this is why i dont like to use Laravel' dependency injection to get the Model in the controller, at least not when dealing with APIs... I like to get the id and search for the Model myself and use custom exceptions, like ModelNotFound, and pass the name of the resource/model that was not found, like "User", which then would be contained in the error message returned to the user, like "User not found"... Its also nice cause u can do things with i18n very easily, like __('exception.model_not_found', ['model' => $model]), where $model could be __("user") ...

gilney.mathias