php - Laravel Eloquent Query 使用 WHERE 一组条件或 OR 另一组条件以及一些常见条件?
问题描述
我必须在我的 Laravel 项目中进行 sql 操作。
除了提到的条件外,我还有一些常见的条件。我知道如何在原始 mysql 中编写它。
所以,例如我需要的是
我怎么说WHERE Condition X AND Condition Y AND ( (a = 1 and b =1 ) OR (c = 1 AND d = 1) )
使用 laravel 查询格式?
解决方案
考虑以下语法:
DB::table('yourTable')
->where('x', '=', '1')
->where('y', '=', '1')
->where(function($q1)
{
$q1->where(function($q2)
{
$q2->where('a', '=', '1')
->where('b', '=', '1');
})
->orWhere(function($q2)
{
$q2->where('c', '=', '1')
->where('d', '=', '1');
});
});
如果您更喜欢 More Readable Syntaxwhere
将接受数组作为参数
DB::table('yourTable')
->where('x','=',1)
->where('y','=',1)
->where(function($queryBuilder){
$queryBuilder->where([
['a','=',1],
['b','=',1],
])
->orWhere([
['c','=',1],
['d','=',1],
]);
});
推荐阅读
- python - 您如何制作进度条并将其放在图像上?
- python - discord.py wait_for message... 如何取消息内容和写消息的人?
- azure - xamarin azure 推送通知 - 如何在 xamarin 中获取 Json 值
- html - 为什么只有一张背景图片在 iPhone 上不可见?
- cookies - 当 Google 废除第 3 方 cookie 时,Facebook 的点赞按钮将如何在第 3 方网站上运行?
- karate - 空手道用户界面 - 当我输入到期日期时,它没有正确使用
- node.js - js_of_ocaml 从 js 调用 ocaml 中的函数
- javascript - 自定义服务器渲染的 Gutenberg 块并不总是渲染
- python-3.x - youtube_dl KeyError 'key'
- php - 使用php在表中显示mysql结果