Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Karim711's avatar

i need to get count of bookings depend on filters but count applied on every separated filter

i need to get count of bookings depend on filters but count applied on every separated filter

 $packagesWithCountBookings = (new Subscription)->withCount(['subscriptionBookings as count_booking'])->newQuery();
        if(isset($request->date)){
            $packagesWithCountBookings = $packagesWithCountBookings->withCount(['subscriptionBookings as count_booking' => function($q) use($date, $date_to){
                $q->whereHas('bookingInfo', function($q2) use($date, $date_to){
                    $q2->whereBetween('booking_date', [$date, $date_to])
                    // ->whereBetween('start_booking_time', [$time, $time_to])
                    ;
                });
            }]);
        }
        
        if(isset($request->time_from)){
            $packagesWithCountBookings = $packagesWithCountBookings->withCount(['subscriptionBookings as count_booking' => function($q) use($time, $time_to){
                $q->whereHas('bookingInfo', function($q2) use($time, $time_to){
                    // $q2->whereBetween('booking_date', [$date, $date_to])
                    $q2->whereBetween('start_booking_time', [$time, $time_to])
                    ;
                });
            }]);
        }

        if(isset($request->zone_id)){
           
            $packagesWithCountBookings = $packagesWithCountBookings->withCount(['subscriptionBookings as count_booking' => function($q) use($zone_id){
                $q->whereHas('bookingInfo', function($q2) use($zone_id){
                    $q2->where('zone_id',$zone_id);
                    ;
                });
            }]);
        }

        if(isset($request->service_id)){
           

             $packagesWithCountBookings = $packagesWithCountBookings->withCount(['subscriptionBookings as count_booking' => function($q) use($filter_segment_service_id){
                    $q->whereHas('bookingInfo', function($q2) use($filter_segment_service_id){
                        $q2->whereHas('services', function($q) use($filter_segment_service_id) {
                            $q->where('segment_service_id', $filter_segment_service_id);
                         });
    
                        ;
                    });
                }]);
        }

        if(isset($request->biker_id)){
           

            $packagesWithCountBookings = $packagesWithCountBookings->withCount(['subscriptionBookings as count_booking' => function($q) use($biker_id){
                $q->whereHas('bookingInfo', function($q2) use($biker_id){
                    $q2->where('biker_id',$biker_id);
                    ;
                });
            }]);
        }

        $packagesWithCountBookings = $packagesWithCountBookings->get();
0 likes
1 reply

Please or to participate in this conversation.