我需要运行这个查询,它在 MYSQL 中工作正常
SELECT * FROM table WHERE title_id = 4 AND (state_a = 'TRUE' OR state_b = 'TRUE' OR state_c = 'TRUE')
我尝试了以下方法以及其他一些选项,但得到的结果似乎是随机的。
$values = \App\Time_Code::select('id','title_id','type','state_a','state_b','state_c')
->where('title_id', '=', '4')
->where('state_a', '=', 'TRUE')
->orWhere('state_b', '=', 'TRUE')
->orWhere('state_c', '=', 'TRUE')
->orderBy('updated_at', 'desc')
->get();
我在这里完全走错方向了吗?在本例中,它包含 title_ids 不为 4 的行。此外,它返回整行,而不仅仅是请求的字段。
最佳答案
您需要编写一个嵌套查询来反射(reflect) MySQL 中的嵌套条件:
$values = \App\Time_Code::select('id','title_id','type','state_a','state_b','state_c')
->where('title_id', '=', '4')
->where(function ($q) {
$q->where('state_a', '=', 'TRUE')
->orWhere('state_b', '=', 'TRUE')
->orWhere('state_c', '=', 'TRUE');
})
->orderBy('updated_at', 'desc')
->get();
请参阅 Parameter Grouping 上的手册.
关于php - 我如何将这个带有多个或的查询转换为 Eloquent 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55437846/