有 findOrFail()
方法,如果没有找到任何内容,它会抛出 404,例如:
User::findOrFail(1);
如何通过自定义列查找实体或失败,如下所示:
Page::findBySlugOrFail('about');
最佳答案
尝试这样:
Page::where('slug', '=', 'about')->firstOrFail();
// or without the explicit '='
Page::where('slug', 'about')->firstOrFail();
关于Laravel - 按自定义列查找或失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29212982/