How to throw a 404 error if a record is not found.
August 9th, 2014Here is an extremely short yet readable way to throw a "page not found" HTTP error when a record is not found in your Laravel app.
public function getPost($id)
{
$post = Article::find($id) or App::abort(404);
return View::make('frontend/post', compact('post'));
}
Laravel is special because it allows us to keep our codebase extremely concise. Since some trade-offs have been made within the framework to allow for that, we should maximize the benefit by writing in a smarter way.