There's findOrFail()
method which throws 404 if nothing was found, e.g.:
User::findOrFail(1);
How can I find an entity by custom column or fail, something like this:
Page::findBySlugOrFail('about');
There's findOrFail()
method which throws 404 if nothing was found, e.g.:
User::findOrFail(1);
How can I find an entity by custom column or fail, something like this:
Page::findBySlugOrFail('about');
Try it like this:
Page::where('slug', '=', 'about')->firstOrFail();
It took at least two hours to realize that if you chain firstOrFail() method after where() in Laravel 5.6, it basically tries to retrieve the first record of the table and removes where clauses. So call firstOrFail before where.
Model::firstOrFail()->where('something', $value)