Laravel - 通过自定义列查找或失败 [英] Laravel - find by custom column or fail

查看:24
本文介绍了Laravel - 通过自定义列查找或失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个 findOrFail() 方法,如果没有找到,则抛出 404,例如:

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

推荐答案

试试这样:

Page::where('slug', '=', 'about')->firstOrFail();    
// or without the explicit '='
Page::where('slug', 'about')->firstOrFail();

这篇关于Laravel - 通过自定义列查找或失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆