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

JhonD's avatar
Level 1

is there is a way to get users count with out pluck ? if i removed pluck it gives me the following error

error ! SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in 'having clause' (SQL: select count(*) as aggregate from users where id not in (67134) and users.deleted_at is null having count = 0)

$device_type=$request->device_type;
                $excludedUsers = [67134];
        
                $date=$request->date;
                $date_to=$request->date_to;
                $time=$request->time_from;
                $time_to=$request->time_to;
                $zone_id=$request->zone_id;
                $biker_id=$request->biker_id;
                $service_id=$request->service_id;
                $user_type=$request->user_type;
                $promotion_type=  $request->promotion_type;
                if($date){
                    $selectedDateFrom = Carbon::parse($date)->toDateString();
                    $selectedDateTo = Carbon::parse($date_to)->endOfDay()->toDateString();
                    
                  
        
                }else{
                    $selectedDateFrom = Carbon::parse($date)->startOfMonth()->toDateString();
                    $selectedDateTo = Carbon::parse($date_to)->endOfMonth()->toDateString();
        
                  
                }
                $sql = "(SELECT COUNT(*) FROM bookings";
                if($selectedDateFrom != null)
                {
                    // dd('test');
                    $sql .= " WHERE bookings.booking_date BETWEEN " . "'$selectedDateFrom'" ." AND " ."'$selectedDateTo'";  
                    // dd($sql);
                }
                // dd($sql);
                
                if($zone_id != null)
                {
                    $sql .= " AND bookings.zone_id = $zone_id";  
                }
                
                if($biker_id != null)
                {
                    $sql .= " AND bookings.biker_id = $biker_id";  
                }
                
                if($time != null)
                {
                    $sql .= " AND bookings.start_booking_time BETWEEN '$time' AND '$time_to'";  
                }

                $sql .= " AND bookings.user_id = users.id) as count";
                // dd($sql);

                $users = User::whereNotIn('id', $excludedUsers)->where(function($q) use($device_type) {
                    if($device_type != null){
                        if($device_type == 1){
                            $q->where('device_type', 'manual');
                        }
                        if($device_type == 2){
                            $q->where('device_type','android');
                        }
                        if($device_type == 3){
                            $q->where('device_type','ios');
                        }
                    }
                })->
             select( [
                //  DB::raw( 'DATE( created_at ) as date' ),
                 DB::raw($sql),
                 
             ] );

            //  dd($users->having('count', '=', 0)->pluck('count')->count());
             dd($users->having('count', '=', 0)->pluck('count')->count());
0 likes
1 reply
mohjacksi's avatar

I think you're trying to use ->sum() insted of ->count() here:

             dd($users->having('count', '=', 0)->pluck('count')->sum());

or you can change this count here to anything else to get a better debugging message :

                $sql .= " AND bookings.user_id = users.id) as count";

Please or to participate in this conversation.