Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - find by custom column or fail

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'); 
like image 578
Limon Monte Avatar asked Mar 23 '15 14:03

Limon Monte


1 Answers

Try it like this:

Page::where('slug', '=', 'about')->firstOrFail();     // or without the explicit '=' Page::where('slug', 'about')->firstOrFail(); 
like image 171
Alupotha Avatar answered Oct 13 '22 12:10

Alupotha