Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should abort(404); be returned?

Using Laravel 5, should abort(404) be returned or will it abort and throw an error and stop my method? I have seen both and both seem to work.

public function show($id)
{
    try {
        $item = Item::where('id',$id)->firstOrFail();
    }
    catch(ModelNotFoundException $exception)
    {
        return abort(404);
    }
    return view('item.show')->with(compact('item'));
}
like image 957
whoacowboy Avatar asked Oct 27 '25 14:10

whoacowboy


2 Answers

firstOrFail() will throw an error if a model is not found. If you want to catch it I would suggest doing it in the global.php

Yo do not need to return the abort() as shown in the documentation

like image 175
Pawel Bieszczad Avatar answered Oct 29 '25 15:10

Pawel Bieszczad


You can abort 404 or 403 error.http://laravel.com/docs/5.0/errors#http-exceptions

abort(404, 'Page Not Found');
like image 44
Imtiaz Pabel Avatar answered Oct 29 '25 15:10

Imtiaz Pabel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!