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.
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.
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/
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 ?
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?
@@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
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)
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.
$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 ?
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
I would use Roles and Permissions for that, not different middlewares. Not for Sanctum specifically, but this video: ruclips.net/video/kZOgH3-0Bko/видео.html
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.
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"); });
@@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.
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.
best youtuber and teacher ever 😎
Thank you for this. Also you can use the ModelNotFound exception to handle the specifically model not found exceptions
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.
@@LaravelDaily oh ok... I will try it and get back to you...
Another aproche, if you use try catch you can catch the specific model not found exception, and then customize the return message
Thanks for the tutorial. Helped me in my production app.
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.
thanks for all videos.
how to make laravel + vue role based authentication
This video is on that topic: ruclips.net/video/JatpAUl6_5E/видео.html
please can you tell us the estimated date of release Mobile App with React Native or Flutter and Laravel API tutorial thanks in advance
Realistically - September.
this video is timely. Please what is the best way to handle validation and other server (5xx) error with API? Thanks
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/
@@LaravelDaily it was helpful, thanks
WOW! Thank you so much, your explanation very well! I'll subscribed :)
Práctico y al punto! Me sirvio mucho. Gracias
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 ?
Thank you , this video really helpful to me
Super! it was helpful for me.
Please can you do a tutorial on Laravel Passport, Using it to authenticate React/Vue Api or Mobile App
I prefer Laravel Sanctum these days, not Passport
I have a question. How I can return a json in laravel api, if user is not authenticated ? I use laravel Sactum
Thanks a lot..you solving my problem
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?
Yes probably
Thank you for your amazing videos. I wish if you can make more videos about API and its advanced tips. Thanks
If you search the channel for "api", there are dozens of videos: ruclips.net/channel/UCTuplgOBi6tJIlesIboymGAsearch?query=api
Thanks a lot
@@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
I'm not sure what would I say, it's just either you create API or you don't :)
@@thelostrider1 API is used with SPA (front-end framework) or mobile app, otherwise you don't need to.
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)
thnax too much , what about error 500 in api ,please.
Thanks a lot but for any error we should have this? Won't it be too much code?
Well, it depends on what results you expect after errors, and how custom you want to go. It's your choice.
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.
You can probably take something from $request and parse it to the "users" word, from URL, for example.
i Love it Thanks you are Awesome keep going man ❤️❤️
$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 ?
I have a doubt. How could I use this in a test. In order to improve the quality of my application ?
Test should probably assert that 404 is returned, not the message.
it worked, thank you
Awesome tutorial
Thanks! Amazing.
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 .
I don't work with JWT, I work with Laravel Sanctum for API Auth.
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
I would use Roles and Permissions for that, not different middlewares.
Not for Sanctum specifically, but this video: ruclips.net/video/kZOgH3-0Bko/видео.html
Thank you 💕
I need to format my API response can you suggest me how I can do it in laravel
Use API resources
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.
You are legend
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");
});
This is great. But the issue i faced with macros is they are not so readable.
@@HassanHafeez007 Write the macros on a separate service provider.
@@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.
This is what I'm looking for. Thank You!
please post video Regarding API LOGS to be stored in table or else...
Thank you! 👍
Thanks
Thx man 🔥
Code like a pro
Good
You are god, but for now only some know.
First 💥
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.
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.
Thanks