Laravel Eloquent 查询类似于 findorfail

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22967553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:20:18  来源:igfitidea点击:

Laravel Eloquent query similar to findorfail

selectlaraveleloquent

提问by Mike

I would like to look up and return an object referenced by a different column to that of id.

我想查找并将不同列引用的对象返回到id.

As far as I know there isn't another method similar to Task::findOrFail($id)but that can reference another field. For example:

据我所知,没有另一种类似于Task::findOrFail($id)但可以引用另一个字段的方法。例如:

Task::findOrFail('column_name' = 'column_data');

Task::findOrFail('column_name' = 'column_data');

I'm currently using

我目前正在使用

Task::where('tid' , '=', $tid)->first();

Task::where('tid' , '=', $tid)->first();

Is this possible?

这可能吗?

回答by Needpoule

Maybe you can use the firstOrFail() function of Laravel.

也许你可以使用 Laravel 的 firstOrFail() 函数。

Task::where('column_name', '=' ,'column_data')->firstOrFail();