php - Laravel Eloquent 根据设定条件查询多条记录
问题描述
我有一个表单,我的用户可以设置条件来搜索他们的客户数据库,但是我正在努力将查询拉到一起(在 Laravel 5.7 中工作)。
目前客户可以设置的标准如下:
- 客户有“确切| 更多 | 少于” 次访问
- 客户第一次访问是“正是 | 更多 | 少于” X 天
- 客户上次访问是“正是 | 更多 | 少于” X 天
- 客户提供商是“facebook | 推特 | 电子邮件 | 任何”</li>
我现在正试图弄清楚如何将其构建到查询中,我什至无法产生一个有形的例子!我的障碍似乎是检查第一条记录和最后一条记录以确保它符合标准。
SQLFiddle:http ://sqlfiddle.com/#!9/68407/1
我的表:
| id | name | email | provider | created_at
---------------------------------------------------------------------------------------
| 1 | Mr Smith | mr.smith@example.com | facebook | 2018-11-01 09:00:00 |
| 2 | Mrs Smith | mrs.smith@example.com | facebook | 2018-11-01 09:00:00 |
| 3 | Miss Smith | miss.smith@example.com | email | 2018-11-01 09:00:00 |
| 4 | Doctor Smith | doctor.smith@example.com | email | 2018-11-01 09:00:00 |
| 5 | Lord Smith | lord.smith@example.com | twitter | 2018-11-01 09:00:00 |
| 6 | Lady Smith | lady.smith@example.com | email | 2018-11-01 09:00:00 |
| 7 | Mr Smith | mr.smith@example.com | facebook | 2018-11-02 09:00:00 |
| 8 | Mrs Smith | mrs.smith@example.com | facebook | 2018-11-02 09:00:00 |
| 9 | Doctor Smith | doctor.smith@example.com | email | 2018-11-02 09:00:00 |
| 10 | Lord Smith | lord.smith@example.com | twitter | 2018-11-02 09:00:00 |
| 11 | Lady Smith | lady.smith@example.com | email | 2018-11-02 09:00:00 |
| 12 | Mr Smith | mr.smith@example.com | facebook | 2018-11-03 09:00:00 |
| 13 | Mrs Smith | mrs.smith@example.com | facebook | 2018-11-03 09:00:00 |
| 14 | Miss Smith | miss.smith@example.com | email | 2018-11-03 09:00:00 |
| 15 | Lord Smith | ord.smith@example.com | twitter | 2018-11-03 09:00:00 |
| 16 | Lady Smith | lady.smith@example.com | email | 2018-11-03 09:00:00 |
查询的示例客户条件:
- 访问次数超过 2 次的客户
- 客户首次访问时间超过 2 天前
- 客户上次访问时间超过 1 天前
- 客户提供商是 Facebook
当前查询:
$Customers = Customer::groupBy('email')
->havingRaw('COUNT(*) > 2’)
->where('created_at', '<', Carbon::now()->subDays(2)->toDateTimeString())
->get();
我不知道如何提取第一个客户记录检查它是否超过 2 天,然后提取他们的最后一条记录并确保它超过 1 天。我知道我当前的查询对于我想要实现的目标完全没有用,但我再次努力将其整合在一起。
预期成绩:
| id | name | email | provider | created_at
---------------------------------------------------------------------------------------
| 12 | Mr Smith | mr.smith@example.com | facebook | 2018-11-03 09:00:00 |
| 13 | Mrs Smith | mrs.smith@example.com | facebook | 2018-11-03 09:00:00 |
解决方案
您要查找的查询是:
select * from customers group by email having count(*) > 2 and min(created_at) <= '2018-10-02 09:00:00' and max(created_at) <= '2018-10-03 09:00:00' and provider = 'facebook'
假设当前时间是 2018-10-04 09:00:00。
雄辩:
$Customers = Customer::groupBy('email')
->havingRaw('COUNT(*) > 2')
->havingRaw('max(created_at) < ?' , [Carbon::now()->subDays(2)->toDateTimeString()])
->havingRaw('min(created_at) < ?' , [Carbon::now()->subDays(1)->toDateTimeString()])
->havingRaw('provider = ?' , ['facebook'])
->get();
另外,使用 eloquent 可以链接方法,如下所示
$customers = Customer::groupBy('email');
if( $includeCount ) {
$customers->havingRaw('COUNT(*) > 2');
}
...
...
...
推荐阅读
- javascript - 如何测试承诺是否返回正确的值
- spring - 如何捕获来自 POST 的值以执行存储在 Spring Boot 中的过程
- c# - 如何正确读取 C# 中 .Any 函数的方法签名
- python - 摄氏到华氏方法不起作用
- c - 使用 fatfs 的 f_printf 的断言错误,但 f_write 工作正常
- java - 如何找到二维数组的总数?
- macos - 为什么“last”的输出显示历史的时间少于我拥有笔记本电脑的时间?
- .net - 如何在数据表的每一列中获取大于 0 的最小字符串长度
- html - 如何将简单的计时器转换为角度 7 中的小时分钟和秒格式
- python - 在 Keras 中保存与训练运行相关的所有数据的好方法是什么?