我想检查 role_mapping 表中变量 role_id 的 OR 条件。我想检查 user_id role_id 是“2”还是“3”。 user_id = 210 的用户有 role_id 2和1.但我的查询结果显示为'3'。如何在Laravel查询中使用或条件?
Role table
user_id role_id
210 2
210 1
$user_role=role_mapping::where('user_id','=',210)
->where('role_id','=','2')
->orwhere('role_id','=','3')
->select('role_mapping.role_id')->first();
echo $user_role->role_id; // print 3
答案 0 :(得分:6)
AND 运算符的优先级高于 OR 运算符,因此您运行的查询是:
Task.Finish
当你应该跑步时
WHERE (user_id = 210 AND role_id = 2) OR role_id = 3
以下将解决这个问题:
WHERE user_id = 210 AND (role_id = 2 OR role_id = 3)