如何在Laravel Eloquent中执行此查询?
SELECT * FROM `ftm_users`, `ftm_students`, `ftm_user_verification` WHERE `ftm_users`.`user_group` = 3 AND `ftm_user_verification`.`verification_status` = 1 AND `ftm_users`.`uid` = `ftm_students`.`uid` AND `ftm_users`.`uid` = `ftm_user_verification`.`uid`
我已经在模型中设置了关系,我已尝试过这个
$userStudent = User::where('user_group', '=', 3)->with(array('userVerification' => function($query) {
$query->where('verification_status', '=', 1);
}, 'student', 'studentParents'))->simplePaginate(20);
但查询使用分离的select语句从不同的表中获取数据。
感谢。
答案 0 :(得分:1)
假设您定义了ftm_users
和ftm_user_verification
之间的关系,您可以使用whereHas
方法过滤相关模型
$userStudent = User::where('user_group', '=', 3)->whereHas('userVerification', function ($query) {
$query->where('verification_status', '=', 1);
})->get();