首页 > 解决方案 > Laravel Query builder查询相当于SQL查询?

问题描述

我的查询在这个 where 子句中运行正常。请告诉我什么会在 Laravel 中查询 builder 查询,相当于这个查询

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) 

标签: mysqllaravellaravel-5query-builderlaravel-query-builder

解决方案


在您的特定情况下,您必须使用嵌套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)

此外,如果列表中只有一个 id,则不需要使用whereIn(or ) 查询。whereNotIn


推荐阅读