如何使用Laravel的Eloquent条款获得过滤结果查询 - 拥有,GroupBy?

时间:2015-11-09 13:29:48

标签: php mysql laravel group-by eloquent

我有3张桌子。他们的结构如下:

1. Table I - listings

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  
id | name | location_id | is_approved | is_bookable  
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  

2. Table II - programmes (Polymorphic - Many to Many)  
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  
id | listing_id | name | base_price | owner_id | owner_type  
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

3. Table III - timeslots (One to Many with programmes)  
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  
id | programme_id | start_time | end_time | discount | inventory  
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

我想获得这些插槽(带有 - 程序和列表),其位置如下:

start_time => '2015-11-10 00:00:00'  
end_time => '2015-11-10 23:59:59'  
inventory => 'greater than 0'  
owner_id => 'equals 1'  
owner_type => 'equals Activity'  
location_id => 'equals 3'

将'programmes.listing_id'分组为'min(price)'

我做了这样一个雄辩的查询: [原始代码]

$query = Timeslot::where('start_time', '>', $start_time)
                            ->where('end_time', '<', $end_time)
                            ->where('inventory', '>', 0)
                            ->whereHas('properties', function($properties_query) use ($timeslot_type) {
                              if(!is_null($timeslot_type)) {
                                $properties_query->where('timeslot_property_id', $timeslot_type);
                              }
                            })
                            ->whereHas('programme', function($programme_query) use ($programme_id, $programme_type, $location_id) {
                              if(!is_null($programme_type) && !is_null($programme_id)) {
                                $programme_query->where('owner_id', $programme_id)
                                                ->where('owner_type', $programme_type);
                              }
                              $programme_query->whereHas('listing', function($listing_query) use ($location_id) {
                                $listing_query->where('is_approved', 1)
                                              ->where('is_bookable', 1)
                                              ->whereHas('location', function($location_query) use ($location_id) {
                                                if(!is_null($location_id)) {
                                                  $location_query->where('id', $location_id);
                                                }
                                              });
                              });
                            })
                            ->join('programmes', 'timeslots.programme_id', '=', 'programmes.id')
                            ->select(['timeslots.*',
                                      DB::raw('(discount/100)*programmes.base_price as discounted_price'),
                                      DB::raw('(programmes.base_price - ((discount/100)*programmes.base_price)) as price')
                                    ])
                            ->with(['programme', 'programme.listing']);

          $query->groupBy('programmes.listing_id')
                ->havingRaw('max(programmes.base_price - ((discount/100)*programmes.base_price))');

          $query->paginate($limit);

[图片代码]

enter image description here

我做了一个像这样的原始查询:

[原始代码]

SELECT `timeslots`.*, 
       ( discount / 100 ) * programmes.base_price 
       AS discounted_price, 
       ( programmes.base_price - 
       ( ( discount / 100 ) * programmes.base_price ) ) AS 
       price 
FROM   `timeslots` 
       INNER JOIN `programmes` 
               ON `timeslots`.`programme_id` = `programmes`.`id` 
WHERE  `start_time` > '2015-11-10 00:00:00' 
       AND `end_time` < '2015-11-10 23:59:59' 
       AND `inventory` > 0 
       AND (SELECT Count(*) 
            FROM   `timeslot_properties` 
                   INNER JOIN `timeslot_property_timeslot_map` 
                           ON `timeslot_properties`.`id` = 
`timeslot_property_timeslot_map`.`timeslot_property_id` 
WHERE  `timeslot_property_timeslot_map`.`timeslot_id` = `timeslots`.`id` 
AND `timeslot_property_id` = 1) >= 1 
AND (SELECT Count(*) 
FROM   `programmes` 
WHERE  `timeslots`.`programme_id` = `programmes`.`id` 
AND `owner_id` = 2 
AND `owner_type` = 'Activity' 
AND (SELECT Count(*) 
FROM   `listings` 
WHERE  `programmes`.`listing_id` = `listings`.`id` 
 AND `is_approved` = 1 
 AND `is_bookable` = 1 
 AND (SELECT Count(*) 
      FROM   `locations` 
      WHERE  `listings`.`location_id` = `locations`.`id` 
             AND `id` = 3) >= 1) >= 1) >= 1 
GROUP  BY `programmes`.`listing_id` 
HAVING Max(price)  

[图片代码]

enter image description here

我没有得到预期的结果! :(
能帮帮我吗?

通过 - '没有得到理想的结果'我的意思是 -
我得到的结果为 -

"timeslots": [
      {
        "id": 127,
        "programme_id": 2,
        "start_time": "2015-11-10 06:00:00",
        "end_time": "2015-11-10 06:30:00",
        "discount": 25,
        "inventory": 200,
        "created_at": "2015-10-19 16:52:18",
        "updated_at": "2015-10-20 16:26:17",
        "discounted_price": "375.0000",
        "price": "1125.0000",
        "programme": {
          "id": 2,
          "listing_id": 30,
          "base_price": 1500,
          "owner_id": 2,
          "owner_type": "Activity",
          "is_activated": 1,
          "created_at": "2015-10-12 15:13:55",
          "updated_at": "2015-10-19 10:45:48",
          "listing": {
            "id": 30,
            "client_id": 42,
            "name": "Listing Name 1",
            "location_id": 3,
            "is_approved": 1,
            "is_bookable": 1,
            "created_at": "2015-10-12 15:13:55",
            "updated_at": "2015-10-28 23:37:20"
          }
        }
      },
      {
        "id": 134,
        "programme_id": 2,
        "start_time": "2015-11-10 06:00:00",
        "end_time": "2015-11-10 06:30:00",
        "discount": 25,
        "inventory": 200,
        "created_at": "2015-10-19 16:52:18",
        "updated_at": "2015-10-20 16:26:17",
        "discounted_price": "700.0000",
        "price": "1400.0000",
        "programme": {
          "id": 2,
          "listing_id": 30,
          "base_price": 1500,
          "owner_id": 2,
          "owner_type": "Activity",
          "is_activated": 1,
          "created_at": "2015-10-12 15:13:55",
          "updated_at": "2015-10-19 10:45:48",
          "listing": {
            "id": 30,
            "client_id": 42,
            "name": "Listing Name 1",
            "location_id": 3,
            "is_approved": 1,
            "is_bookable": 1,
            "created_at": "2015-10-12 15:13:55",
            "updated_at": "2015-10-28 23:37:20"
          }
        }
      }
    ]

在上面的结果中,我的两个列表都相同(列表id:30在timeslots_id中:127,134)。我希望查询应该通过在时隙数组内基于最小价格属性对列表进行分组来给出这些结果。!!

0 个答案:

没有答案