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

samalapsy's avatar

Select All from table and group by specific column

I want to select all the entries in the referrals table and them group by referral

$referrals = Booking::whereNotNull('referral')->groupBy('bookings.referral')->orderBy('referral_income', 'DESC')->get();

This is the error I got

SQLSTATE[42000]: Syntax error or access violation: 1055 'bookings.id' isn't in GROUP BY (SQL: select * from `bookings` where `referral` is not null and `bookings`.`deleted_at` is null group by `bookings`.`referral` order by `referral_income` desc)

Please note that my Referral column has been made an index on the database.

Bookings Table structure

id, room_id, user_id, transaction_id, reference, price, discount_price, referral_income, referral, status

Thank you

0 likes
12 replies
samalapsy's avatar

Someone should kindly help me out with this

Table Structure

id, room_id, user_id, transaction_id, reference, price, discount_price, referral_income, referral, status
Snapey's avatar

try removing bookings. from ->groupBy('bookings.referral')-

Snapey's avatar

Do you have a $with statement in your model?

samalapsy's avatar

No I don't have it. This is my model

<?php

namespace App;
use App\User;
use App\Hostel;
use Mail;
use App\Mail\NewBooking;
use App\Mail\BookingSucessful;
use App\Mail\BookingFailed;
use App\Mail\Invoice;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Booking extends Model {
    
    use SoftDeletes;

    protected $fillable =['user_id', 'hostel_id', 'transaction_id', 'user_type', 'inspection_date', 'move_in_date', 'cleaning_services', 'information', 'status', 'room', 'price', 'discount_price','referral_income','referral', 'channel', 'reference' ];

    protected $dates = ['deleted_at'];

    public function user() {
        return $this->belongsTo(User::class);
    }

    public function getOwnerPhone($id) {
        $user = User::select('phone')->whereId($id)->first();
        return $user->phone;
    }

    public function getOwnerEmail($id) {
        $user = User::select('email')->whereId($id)->first($id);
        return $user->email;
    }

    public function getBookerPhone($id) {
        $user = User::select('phone')->whereId($id)->first();
        return $user->phone;
    }

    public function getBookerEmail($id) {
        $user = User::select('email')->whereId($id)->first($id);
        return $user->email;
    }

    public function getBookedHostel($id) {
        return $hostel = Hostel::find($id);
    }
    
    public function booker() {
        return  $this->belongsTo(User::class);
    }

    
    public function hostel() {
        return $this->belongsTo(Hostel::class);
    }


    public function referalIncome($refId) {
        return $refIncome= Booking::where('referral', $refId)->sum('referral_income');

    }

    public function hostelDetails($id) {
        return Hostel::find($id);
    }

    public function finalizeBooking($data){
        
    }
    
    public function successfulBookingMail($data) {
        return Mail::to($data['user_email'])->send(new BookingSucessful($data));
    }

    public function successfulBookingToOwner($data) {
        return Mail::to($data['user_email'])->send(new NewBooking($data));
    }
    

    public function failedBookingMail($data) {      
        return Mail::to($data['user_email'])->send(new BookingFailed($data));
    }


    public function successfulInvoiceMail($data) {
        return Mail::to($data['user_email'])->send(new Invoice($data));
    }

    
}

If there's a way I can optimize this model I'll be glad to.

extjac's avatar

try this and show me the error.

$referrals = Booking::groupBy('referral')->get();
mikefolsom's avatar

Try editing your config/database.php file:

In connections > mysql, set 'strict' => false and see if that helps.

bmm's avatar

@samalapsy I know it is a 11 month old post. Just checking, did you manage to find the solution for this?

AlphaXder's avatar

i know it is late but try $referrals = Booking::whereNotNull('referral')->orderBy('referral_income', 'DESC')->get()->groupBy('referral');

1 like

Please or to participate in this conversation.