how to retrieve some columns along with count of a column - genders value in laravel?

Viewed 62

I am using Laravel excel fromQuery() to export data in excel. In addition to the data obtained from the given query, I also want to get gender wise count (i.e., total number of male,female,transgender) by using single query.

  public function __construct($from, $to, $socialGroup, $gender)
        {
            $this->from = $from;
            $this->to = $to;
            $this->socialGroup = $socialGroup;
            $this->gender = $gender;
        }
        /**
         * @return \Illuminate\Support\Collection
         */
        public function query()
        {
           $data = RegistrationSchoolCollegeStudents::select(
                'registration_number',
                'course_name',
                'school_name',
                'class',
                'fullname',
                'gender',
                'dob',
                'street_road_lane',
                'area_locality_road',
                'village_town_city',
                'district',
                'pincode',
                'email',
                'social_group',
                'adhaar_number',
                'certificate_number',
                'is_own_smart_phone',
                'created_at',
            );
        
            if (
                $this->from == 'null' &&
                $this->to == 'null' &&
                $this->socialGroup == 'null' &&
                $this->gender == 'null'
            ) {
                dd($data->get());
            }
        
            if ($this->from !== 'null' && $this->to !== 'null') {
                $data->whereBetween('created_at', [$this->from, Carbon::parse($this->to)->endOfDay()]);
            }
        
            if ($this->socialGroup !== 'null') {
                $data->where('social_group', '=', $this->socialGroup);
            }
        
            if ($this->gender !== 'null') {
                $data->where('gender', '=', $this->gender);
            }
            dd( $data->get());
//return $data;
    }
0 Answers
Related