Laravel - find by custom column or fail

Limon Monte picture Limon Monte · Mar 23, 2015 · Viewed 87.3k times · Source

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');

Answer

Alupotha picture Alupotha · Mar 23, 2015

Try it like this:

Page::where('slug', '=', 'about')->firstOrFail();