如何使用 table 和 Laravel 5 的相关条件查询 table
How do I query a table with conditions from related table with Laravel 5
我有两个table,
table 用户:
身份证、姓名等
table 视频:
id, user_id, 等等
型号:视频
public function user()
{
return $this->belongsTo('App\Models\User');
}
我想 select 用户拥有的所有视频 "Max"。
我尝试过如下操作:
$Videos = \App\Models\Video::with('user')->where('user.name','=','Max')->get();
Unknown column 'user.name' in 'where clause' (SQL: select * from `videos` where `videos`.`deleted_at` is null and `user`.`name` = Max)
谢谢!
这就是 whereHas
的用途:
$Videos = \App\Models\Video::with('user')->whereHas('user', function($q){
$q->where('name','=','Max');
})->get();
我有两个table,
table 用户: 身份证、姓名等
table 视频: id, user_id, 等等
型号:视频
public function user()
{
return $this->belongsTo('App\Models\User');
}
我想 select 用户拥有的所有视频 "Max"。
我尝试过如下操作:
$Videos = \App\Models\Video::with('user')->where('user.name','=','Max')->get();
Unknown column 'user.name' in 'where clause' (SQL: select * from `videos` where `videos`.`deleted_at` is null and `user`.`name` = Max)
谢谢!
这就是 whereHas
的用途:
$Videos = \App\Models\Video::with('user')->whereHas('user', function($q){
$q->where('name','=','Max');
})->get();