I just create a 404 error blade file, and Laravel automatically returns that whenever a model is not found.
A good way to handle findOrFail exception?
Wonder how people handle findOrFail when if it fails. What would be the clean way
Yeah, 404 page is usually great idea. You can also do whatever you want, I like to have my exception handler (in app/Exceptions/Handler.php folder) set to handle different exceptions nicely and globally. If I want to have some specific behavior, I use try ... catch block. If I want to do something on a model, but it's not a disaster when it's not there, I use find() instead of findOrFail() and check for null.
so findOrFail return NotFoundHttpException , is there a clean way to handle this exception for a certain model and not all models? For instance in the User model, I would throw UserNotFoundException rather than just NotFoundHttpException . I don't like to wrap my code in try catch, although sometimes I am forced to do so! With try catch I can easily achieve that but correct me if I am wrong about not liking to use Try catch if you think that is a clean approach!
Imo if your needing a requirement on only a few models. Catch the exception in Laravel (you can make/override exceptions of certain types in Laravel). And the others either create your own exceptions (use find not findOrFail()) and of null throw your custom exception.
Ultimately it's up to you on where and how. Your building it and know all the details you can't convey on a forum.
Please or to participate in this conversation.