如何使用或在laravel查询中制作和调整

时间:2017-08-19 09:10:25

标签: laravel-5

我的查询如下

SELECT * FROM `user_register` 
INNER JOIN `locationdetail` on `locationdetail`.`userid` = `user_register`.`id` 
INNER JOIN `lifestyle` on `lifestyle`.`userid` = `user_register`.`id` 
WHERE `lifestyle`.`drink` in (2) and 
( `locationdetail`.`state_id` in (4121) or `locationdetail`.`country_id` in (38))

在此查询中,如何在laravel模型中进行括号内部查询

1 个答案:

答案 0 :(得分:1)

抱歉分心了

    $drinks = [1];
    $state_ids = [1,2]
    $country_ids = [1,2,3]

    $someModel
    ->join() // enter your joins here
    ->whereIn('lifestyle.drink', $drinks)
    ->where(function( $q1 ) use ($state_ids, $country_ids) {
       // insert the whereOr queries here against the $q1 using the data in `use` params 
    })
    ->get();

要检查您的查询,您还可以在laravel中使用toSql()功能。