Laravel 选择一个where子句雄辩的列

Laravel 选择一个where子句雄辩的列,laravel,eloquent,Laravel,Eloquent,我用的是雄辩的语言。但我很难理解雄辩的语法。我一直在寻找,并尝试这个备忘单:,但没有运气 如何执行此SQL查询? 从note\u id=1的notes中选择user\u id 谢谢 如果需要一条记录,请使用 Note::where('note_id','1')->first(['user_id']); 并用于多个记录使用 Note::where('note_id','1')->get(['user_id']); 如果您想要一条记录,请使用 Note::where('note_id

我用的是雄辩的语言。但我很难理解雄辩的语法。我一直在寻找,并尝试这个备忘单:,但没有运气

如何执行此SQL查询?
从note\u id=1的notes中选择user\u id


谢谢

如果需要一条记录,请使用

Note::where('note_id','1')->first(['user_id']);
并用于多个记录使用

Note::where('note_id','1')->get(['user_id']);

如果您想要一条记录,请使用

Note::where('note_id','1')->first(['user_id']);
并用于多个记录使用

Note::where('note_id','1')->get(['user_id']);

如果“note_id”是模型上的主键,您可以简单地使用:

Note::find(1)->user_id
否则,可以使用任意数量的语法:

Note::where('note_id', 1)->first()->user_id;
Note::select('user_id')->where('note_id', 1)->first(); 
Note::whereNoteId(1)->first();
// or get() will give you multiple results if there are multiple
另外请注意,在这些示例中,您也可以将整个对象分配给一个变量,并在以后需要时获取user_id属性

$note = Note::find(1);
// $user_id = $note->user_id;

如果“note_id”是模型上的主键,您可以简单地使用:

Note::find(1)->user_id
否则,可以使用任意数量的语法:

Note::where('note_id', 1)->first()->user_id;
Note::select('user_id')->where('note_id', 1)->first(); 
Note::whereNoteId(1)->first();
// or get() will give you multiple results if there are multiple
另外请注意,在这些示例中,您也可以将整个对象分配给一个变量,并在以后需要时获取user_id属性

$note = Note::find(1);
// $user_id = $note->user_id;

那个SQL查询是完全无效的,因为它缺少一个
FROM
子句。很可能你想要像
Model::select('user\u id')->whereNoteId(1)->get()
。那个SQL查询是完全无效的,因为它缺少
FROM
子句。很可能你想要像
Model::select('user\u id')->whereNoteId(1)->get()