Laravel 查询生成器查询等同于 SQL 查询?
Laravel Query builder query equivalent to SQL query?
我的查询工作正常,只是这个 where 子句中的问题。请告诉我 Laravel 中的查询构建器查询,相当于此查询
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)
在您的特定情况下,您必须使用嵌套的 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
(或 whereNotIn
)查询。
我假设它将是 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})
我的查询工作正常,只是这个 where 子句中的问题。请告诉我 Laravel 中的查询构建器查询,相当于此查询
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)
在您的特定情况下,您必须使用嵌套的 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
(或 whereNotIn
)查询。
我假设它将是 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})