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
  • ХоббиХобби

Комментарии • 25

  • @hakankurtcom
    @hakankurtcom 4 месяца назад

    Very nice explanation and good scenario solving about Model Bind Implementation catch Exception

  • @dmytro.seredinov
    @dmytro.seredinov Год назад +4

    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.

  • @chlouis-girardot
    @chlouis-girardot Год назад

    Welcome back Sir ! Happy to see you in good shape 💪

  • @pierdocks5412
    @pierdocks5412 Год назад

    Great example, I was just getting to that question on how to. Thank you .

  • @abhiyustho6247
    @abhiyustho6247 Год назад +1

    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 🙏.

  • @guramaylie
    @guramaylie Год назад

    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.

    • @LaravelDaily
      @LaravelDaily  Год назад

      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

  • @ZamirMubashir
    @ZamirMubashir Год назад

    Cool

  • @JohnnyBigodes
    @JohnnyBigodes Год назад +1

    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.

  • @AndreiAndrei-ln8xb
    @AndreiAndrei-ln8xb Год назад

    Welcome back, I missed your videos.
    Not a big fan of series of video on same subject ex Livewire.

    • @LaravelDaily
      @LaravelDaily  Год назад

      Yeah, me too, Livewire series was just because I had been traveling and didn't have THAT many videos shot in advance.

  • @aribiali3574
    @aribiali3574 Год назад

    Good morning

  • @SanderCokart
    @SanderCokart Год назад

    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 🤣

    • @LaravelDaily
      @LaravelDaily  Год назад +1

      You can load relationship later, with $model->load(relationship name) if I understood you correctly

    • @SanderCokart
      @SanderCokart Год назад

      @@LaravelDaily no not exactly, I use a where clause while using ::with() instead of whereBelongTo(Model $binding)

    • @SanderCokart
      @SanderCokart Год назад

      @@LaravelDaily so instead of Model $model as method params you'd have normal int|string $id|$somethingElse

    • @LaravelDaily
      @LaravelDaily  Год назад +1

      Interesting. Well if it works for you, cool!

  • @user-rj7du7rb5n
    @user-rj7du7rb5n Год назад

    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)
    {
    }

  • @kkodingg
    @kkodingg Год назад +2

    I'm surprised that php has the match keyword. I initially thought it only exists in rust.

  • @MiguelAngelSS4
    @MiguelAngelSS4 Год назад +2

    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.

    • @elomenaidise5229
      @elomenaidise5229 Год назад +6

      Yes that can also be handled same way by simply checking if it expects json.

    • @MiguelAngelSS4
      @MiguelAngelSS4 Год назад +4

      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);
      }

  • @saytama95
    @saytama95 Год назад

    not recommend to do it

  • @upravdelami
    @upravdelami Год назад

    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.