laravel查询生成器查询等价于sql查询?

k75qkfdt  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(373)

我的查询运行良好,这是where子句中唯一的问题。请告诉我什么将查询生成器查询在拉威尔,相当于这个查询

WHERE (interest_request.sender_id = 6 or interest_request.to_user_id = 6) 
AND interest_request.interest_status =2 
and profiles.profile_id not in (6)

我使用以下查询生成器,但不工作

where('to_user_id',$my_profile_id)->orwhere
('sender_id',$my_profile_id)->where('interest_status','2')
->whereNotIn('profiles.profile_id', $my_profile_id)
93ze6v8z

93ze6v8z1#

在您的特定情况下,必须使用嵌套 where 条款:

->where(function ($query) use ($my_profile_id) {
    $query->where('to_user_id', $my_profile_id)
        ->orWhere('sender_id', $my_profile_id);
})
->where('interest_status', 2)
->where('profile_id', '!=', $my_profile_id)

你也不需要使用 whereIn (或 whereNotIn )如果列表中只有一个id,则进行查询。

yrefmtwq

yrefmtwq2#

我假设在sql中是这样的:

WHERE to_user_id = $my_profile_id  
OR sender_id = $my_profile_id
AND interest_status = 2
AND profiles.profile_id NOT IN ($my_profile_id)

当我使用orwhere()语句时,我使用回调函数:

where(function($query) {$query->something})
->orwhere(function($query) {$query->otherOptions})

相关问题