0

I am building an event website with laravel, my problem is that I want to query the event table in the DB and also organizers table and ticket table, the event table will provide the id which I will use to query the rest of the tables (tickets and organizers) which is linked with the events_id that is in both tickets and organizers

below is the code

    $events = Events::orderBy('id', 'desc')
    ->leftJoin('organizers', 'events.id', '=', 'organizers.events_id')
    ->leftJoin('tickets', 'events.id', '=', 'tickets.events_id')
    ->paginate(env('EventPag'));

here is the code for the events model

public function organizers(){
    return $this->hasMany('App\Organizers');
}
public function tickets(){
    return $this->hasMany('App\Tickets');
}

here is the code for the organizers model

public function events(){
    return $this->belongsTo('App\Events');
}

it gave me this error

Illuminate \ Database \ QueryException (42000)

SQLSTATE[42000]: Syntax error or access violation: 1055 'obj.events.name' isn't in GROUP BY (SQL: select events.* from events left join organizers on events.id = organizers.events_id left join tickets on events.id = tickets.events_id group by events.id order by events.id desc limit 6)

how can I rectify this error?

Community
  • 1
  • 1
Eloike David
  • 175
  • 4
  • 15

1 Answers1

0

If you're using eloquent correctly

Just fix your get in controller

$events = Events::orderBy('id', 'desc')
    ->with(['organizers','events'])
    ->paginate(env('EventPag'));

For more info follow this link: Laravel - Eloquent relationship.

Thiago Valente
  • 673
  • 8
  • 25
  • What I want is that for every event, it will load every ticket and organizers, but with this your solution, it will load only one ticket and organizer – Eloike David Aug 12 '19 at 20:41