Laravel API 404 Error: Customize Exception Message

Поделиться
HTML-код
  • Опубликовано: 16 ноя 2024

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

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

    best youtuber and teacher ever 😎

  • @AbdulKudusIssah
    @AbdulKudusIssah 3 года назад +1

    Thank you for this. Also you can use the ModelNotFound exception to handle the specifically model not found exceptions

    • @LaravelDaily
      @LaravelDaily  3 года назад +2

      I've actually tried it, while preparing for this video. For some reason, ModelNotFound is not thrown, only HttpNotFound. Maybe they changed it in some Laravel version.

    • @AbdulKudusIssah
      @AbdulKudusIssah 3 года назад

      @@LaravelDaily oh ok... I will try it and get back to you...

  • @TheMarcelitto
    @TheMarcelitto 2 года назад

    Another aproche, if you use try catch you can catch the specific model not found exception, and then customize the return message

  • @isururanawaka5839
    @isururanawaka5839 2 года назад +1

    Thanks for the tutorial. Helped me in my production app.

  • @alexprezmon6951
    @alexprezmon6951 2 года назад

    Thanks for the video. I'm having trouble trying to implement this. I did exactly what you did and always get this response in Postman:
    "message": "Attempt to read property \"id\" on null",
    "exception": "ErrorException",
    What should I do. Thank you.

  • @muhammedrashidp
    @muhammedrashidp 3 года назад

    thanks for all videos.
    how to make laravel + vue role based authentication

    • @LaravelDaily
      @LaravelDaily  3 года назад

      This video is on that topic: ruclips.net/video/JatpAUl6_5E/видео.html

  • @mohamedhosni6271
    @mohamedhosni6271 3 года назад +3

    please can you tell us the estimated date of release Mobile App with React Native or Flutter and Laravel API tutorial thanks in advance

  • @victoronwe906
    @victoronwe906 3 года назад

    this video is timely. Please what is the best way to handle validation and other server (5xx) error with API? Thanks

    • @LaravelDaily
      @LaravelDaily  3 года назад

      Not sure what do you mean by "handle" but here's my article from a few years ago: laraveldaily.com/laravel-api-errors-and-exceptions-how-to-return-responses/

    • @victoronwe906
      @victoronwe906 3 года назад

      @@LaravelDaily it was helpful, thanks

  • @leslysuarez9686
    @leslysuarez9686 3 года назад +2

    WOW! Thank you so much, your explanation very well! I'll subscribed :)

  • @yuslinandev
    @yuslinandev 2 года назад +2

    Práctico y al punto! Me sirvio mucho. Gracias

  • @parimalbhattarai3920
    @parimalbhattarai3920 2 года назад

    if we have get /users/{id}/roles/{role_id}, object not found does not provide enough info like weather role is not found or user not found ? how can we solve this ?

  • @kurdev101
    @kurdev101 10 месяцев назад

    Thank you , this video really helpful to me

  • @СергейГолов-ш2к
    @СергейГолов-ш2к 3 года назад +2

    Super! it was helpful for me.

  • @stancobridge
    @stancobridge 3 года назад +2

    Please can you do a tutorial on Laravel Passport, Using it to authenticate React/Vue Api or Mobile App

    • @LaravelDaily
      @LaravelDaily  3 года назад +1

      I prefer Laravel Sanctum these days, not Passport

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

    I have a question. How I can return a json in laravel api, if user is not authenticated ? I use laravel Sactum

  • @pratikdesai7623
    @pratikdesai7623 3 года назад

    Thanks a lot..you solving my problem

  • @tomjerry-nx9dl
    @tomjerry-nx9dl Год назад

    if someone forgots to put accep: application/json on the header, can we do this? - create a middleware and add accept: application/json on the header and apply that middleware to all api. is this way right?

  • @abdelwahabs.301
    @abdelwahabs.301 3 года назад

    Thank you for your amazing videos. I wish if you can make more videos about API and its advanced tips. Thanks

    • @LaravelDaily
      @LaravelDaily  3 года назад +1

      If you search the channel for "api", there are dozens of videos: ruclips.net/channel/UCTuplgOBi6tJIlesIboymGAsearch?query=api

    • @abdelwahabs.301
      @abdelwahabs.301 3 года назад

      Thanks a lot

    • @thelostrider1
      @thelostrider1 3 года назад

      @@LaravelDaily Maybe a video on the main differences between developing for API or non-API. When I see other people developing for API it all just looks the same... Didn't find a video like that, if you have something similar then sorry :P

    • @LaravelDaily
      @LaravelDaily  3 года назад +1

      I'm not sure what would I say, it's just either you create API or you don't :)

    • @abdelwahabs.301
      @abdelwahabs.301 3 года назад

      @@thelostrider1 API is used with SPA (front-end framework) or mobile app, otherwise you don't need to.

  • @soniablanche5672
    @soniablanche5672 3 года назад +1

    The reason why it sends html if Accept: application/json is not set is because there a bunch of Laravel code that looks like this.
    request()->wantsJson() : (send json) ? (send html)

  • @abdelrazeksayedmohmmed2891
    @abdelrazeksayedmohmmed2891 3 года назад

    thnax too much , what about error 500 in api ,please.

  • @mohammadmohammadi2724
    @mohammadmohammadi2724 3 года назад

    Thanks a lot but for any error we should have this? Won't it be too much code?

    • @LaravelDaily
      @LaravelDaily  3 года назад

      Well, it depends on what results you expect after errors, and how custom you want to go. It's your choice.

  • @dariusdxd
    @dariusdxd 3 года назад

    Hi. Is it possible in easy way to customize Handler error message? If I want to return error related with users, but not show the general message. Thanks.

    • @LaravelDaily
      @LaravelDaily  3 года назад +1

      You can probably take something from $request and parse it to the "users" word, from URL, for example.

  • @Mohamadkhalaf
    @Mohamadkhalaf 3 года назад +1

    i Love it Thanks you are Awesome keep going man ❤️❤️

  • @yotsusan_machi
    @yotsusan_machi 2 года назад

    $validate=$request->validate([
    "name"=>['required','min:3','max:255'],
    "hobby"=>['required','min:3','max:255'],
    "address"=>['required','min:3','max:255']
    ]);
    how to make response json if the validate is error
    example if the input filed name is "ab" i want to make response json (message => "name field must more than 3 character") how can i do that in laravel 9 ?

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

    I have a doubt. How could I use this in a test. In order to improve the quality of my application ?

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

      Test should probably assert that 404 is returned, not the message.

  • @thuythanhvongu
    @thuythanhvongu 2 года назад

    it worked, thank you

  • @bestsolution794
    @bestsolution794 3 года назад

    Awesome tutorial

  • @ssapeiron
    @ssapeiron 3 года назад +1

    Thanks! Amazing.

  • @bestsolution794
    @bestsolution794 3 года назад +1

    Can you please make a tutorial on JWT token package for multi auth in api
    Because i am trying to implement jwt package but it's not creating a token .

    • @LaravelDaily
      @LaravelDaily  3 года назад +4

      I don't work with JWT, I work with Laravel Sanctum for API Auth.

    • @bestsolution794
      @bestsolution794 3 года назад +1

      Okay can we use Sanctum for multi auth like one middleware for super admin another middleware for teachers and third is for users
      If there is way ..then please suggest how we can do that

    • @LaravelDaily
      @LaravelDaily  3 года назад

      I would use Roles and Permissions for that, not different middlewares.
      Not for Sanctum specifically, but this video: ruclips.net/video/kZOgH3-0Bko/видео.html

    • @bestsolution794
      @bestsolution794 3 года назад

      Thank you 💕

  • @buhahemal4635
    @buhahemal4635 3 года назад

    I need to format my API response can you suggest me how I can do it in laravel

  • @MrDragos360
    @MrDragos360 3 года назад

    what happened to the try catch structure like this ?
    try {
    // search for data
    // do other stuff
    } catch (\ModelNotFoundException $e) {
    return response()->json(['message' => 'error message', ....]);
    } ... other catch blocks
    ?
    In this way you can provide a custom not found message. For example you can have: "Product not found" or "Category not found" or whatever.
    Also why not have 2 controllers, one for API and one for WEB, and have the logic inside a service class ? This makes the project structure be more clear than cheking if the request is for an api or for a web.

  • @HadayatNiazi-xp1fz
    @HadayatNiazi-xp1fz Год назад

    You are legend

  • @alamin_firdows
    @alamin_firdows 3 года назад +4

    I add two macros to the Builder class "findOrFailWithMessage" and "firstOrFailWithMessage".
    Builder::macro('findOrFailWithMessage', function (string $id, string $message = null) {
    return $this->find($id) ?: abort(404, $message ?: $this->getModel()::NOT_FOUND_MESSAGE ?: "Resource could not be found");
    });

    • @HassanHafeez007
      @HassanHafeez007 3 года назад +1

      This is great. But the issue i faced with macros is they are not so readable.

    • @alamin_firdows
      @alamin_firdows 3 года назад

      @@HassanHafeez007 Write the macros on a separate service provider.

    • @HassanHafeez007
      @HassanHafeez007 3 года назад +1

      @@alamin_firdows yes I know the convention. I just shared my opinion. In my early days when i worked on someone else code i didn't know about macros and then i searched the function name that was infact a macro. On google i got nothing.

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

      This is what I'm looking for. Thank You!

  • @amitdev1485
    @amitdev1485 3 года назад

    please post video Regarding API LOGS to be stored in table or else...

  • @dadeke
    @dadeke 2 года назад

    Thank you! 👍

  • @GergelyCsermely
    @GergelyCsermely 3 года назад

    Thanks

  • @llBestBoyll
    @llBestBoyll 2 года назад

    Thx man 🔥

  • @amiranbari9637
    @amiranbari9637 3 года назад

    Code like a pro

  • @diatm1506
    @diatm1506 3 года назад

    Good

  • @EduardR10
    @EduardR10 2 года назад

    You are god, but for now only some know.

  • @bariqdharmawan4680
    @bariqdharmawan4680 3 года назад +1

    First 💥

  • @ward7576
    @ward7576 2 года назад +1

    Let's face it, there's no need to try to hide your backend framework, it's just too easy to detect either way. It's a small hurdle, at best.

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

      The objective is not only to hide the backend tech but also to provide more human-friendly error messages. So to speak, I do agree with you that the backend tech can be detected easily using some tools or whatsoever, but I think you get the idea.

  • @Semicolon..
    @Semicolon.. Год назад

    Thanks