In my controller, I used the method firstOrFail() for a database query with Eloquent. When in debug mode, I get a Laravel error message with the content IlluminateSupportItemNotFoundException.
However, I’d like to return a redirect() or mabye back() instead of showing this error screen. How can I do that?
Advertisement
Answer
Instead of firstOrFail() use first() and use condition to redirect back, for example:
$item = Item::where('slug', $slug)->first();
if (! $item) {
return redirect()->back();
}