首页 > 解决方案 > Laravel Eloquent 根据设定条件查询多条记录

问题描述

我有一个表单,我的用户可以设置条件来搜索他们的客户数据库,但是我正在努力将查询拉到一起(在 Laravel 5.7 中工作)。

目前客户可以设置的标准如下:

我现在正试图弄清楚如何将其构建到查询中,我什至无法产生一个有形的例子!我的障碍似乎是检查第一条记录和最后一条记录以确保它符合标准。

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 | 

查询的示例客户条件:

当前查询:

    $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 | 

标签: phpmysqllaraveleloquent

解决方案


您要查找的查询是:

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');
}
...
...
...

推荐阅读