Laravel Exceptions "Trick": Route Model Binding and Model Not Found
HTML-код
- Опубликовано: 31 мар 2023
- In this video, I will improve an example I found on Twitter: we will replace a Service class with Route Model Binding, and process the exception for various Models.
Original tweet: / 1635401809710972928
- - - - -
Support the channel by checking out my products:
- My Laravel courses: laraveldaily.com/courses
- Laravel QuickAdminPanel: quickadminpanel.com
- Livewire Kit Components: livewirekit.com
- - - - -
Other places to follow:
- My weekly Laravel newsletter: us11.campaign-archive.com/hom...
- My personal Twitter: / povilaskorop - Хобби
Very nice explanation and good scenario solving about Model Bind Implementation catch Exception
One minor 'improvement': instead of manually matching the Model class, we could do something like 'class_basename($e->getModel())' so the output will be identical.
Cheers, Dmytro Seredinov.
Welcome back Sir ! Happy to see you in good shape 💪
Great example, I was just getting to that question on how to. Thank you .
Rather than using match, we can use get_class to get the current model class, then explode by / and get the last array which is contain the model name. So we dont need add manually model name again 🙏.
Hello, I wanted to start by thanking you for all of the amazing content that you've put out so far. Your tutorials have been incredibly helpful in my journey to learn Laravel! I was wondering if you might consider making some more videos specifically focused on Laravel Breeze in the future? I'm eager to learn more, and I know that your insights would be invaluable.
What exactly would be your questions about Breeze? It's a simple starter kit, and then you build custom code on top. I have a lot of content about Breeze on laraveldaily.com: laraveldaily.com/tag/auth-breeze
Cool
I like the improvements but I am not sure if everything is correct. In the tweet the guy is using a repository pattern to retrieve the user and you are suggesting to retrieve the user directly from a model. These are different things. Besides that I like the way you handle exceptions are it should be like you did.
Welcome back, I missed your videos.
Not a big fan of series of video on same subject ex Livewire.
Yeah, me too, Livewire series was just because I had been traveling and didn't have THAT many videos shot in advance.
Good morning
I have typically stayed away from route model binding so I can eagerload it in a single query making the binding not needed. Its personal preference obviously but can save you if you didn't index said column for the bind and its a slower query. If any at all disagree with this please shoot me down 🤣
You can load relationship later, with $model->load(relationship name) if I understood you correctly
@@LaravelDaily no not exactly, I use a where clause while using ::with() instead of whereBelongTo(Model $binding)
@@LaravelDaily so instead of Model $model as method params you'd have normal int|string $id|$somethingElse
Interesting. Well if it works for you, cool!
i was use routeServiceProvider: public function boot()
{
parent::boot();
Route::bind('user', function ($value) {
return User::where('user_name', $value)->first() ?? abort(404);
});
} and in the controller i have public function index(User$user)
{
}
I'm surprised that php has the match keyword. I initially thought it only exists in rust.
I like all this improvements, but I feel that something is missed: what if you have an API inside the project? You probably want a json response, not a view for this 404 error.
Yes that can also be handled same way by simply checking if it expects json.
I think this could be done by something like this:
public function render($request, Throwable $exception)
{
if ($exception instanceof ModelNotFoundException) {
if ($request instanceof Request) {
return response()->view('web.error', [], Response::HTTP_NOT_FOUND);
} elseif ($request instanceof JsonResponse) {
return response()->json(['message' => 'Model not found.'], Response::HTTP_NOT_FOUND);
}
}
return parent::render($request, $exception);
}
not recommend to do it
Hi, for the "Advanced Laravel Livewire" playlist, please tell me how to set focus to another element (select or text field) after button click or parent select change. In your "2-Level Parent-Child Dependent Dropdowns" example, after selecting "Country" the "City" select is not autofocused. I want to have fewer mouse clicks.
And thank you very much for your daily work.