简体   繁体   English

Laravel5 Eloquent从具有where子句的多个表中进行选择

[英]Laravel5 Eloquent select from multiple table with where clause

How to do this query in Laravel Eloquent? 如何在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`

I already set the relationship in Model and I have tried with this 我已经在模型中设置了关系,我已尝试过这个

$userStudent = User::where('user_group', '=', 3)->with(array('userVerification' => function($query) {
        $query->where('verification_status', '=', 1);
    }, 'student', 'studentParents'))->simplePaginate(20);

but the query is using separated select statement to get data from different table. 但查询使用分离的select语句从不同的表中获取数据。

Thanks. 谢谢。

Assuming that you defined the relationships between ftm_users and ftm_user_verification , you can use the whereHas method to filter related models 假设您定义了ftm_usersftm_user_verification之间的关系,您可以使用whereHas方法过滤相关模型

$userStudent = User::where('user_group', '=', 3)->whereHas('userVerification', function ($query) {
    $query->where('verification_status', '=', 1);
})->get();

Check Querying Relationship Existence in the docs 检查文档中的查询关系存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM