I have three tables, here are the relevant columns:
events
id
name
location_id
org_id
locations
id
name
orgs
id
name
Here is the query builder in my controller:
$events = DB::table('events')
->join ('locations', 'events.location_id', '=', 'locations.id')
->join('orgs', 'events.org_id', '=', 'orgs.id')
->select('events.id', 'events.name', 'orgs.name', 'locations.name')
->get();
return response()->json($events);
And the output is thus:
[{"id":"2","name":"HUB 123"}, {"id":"3","name":"HUB Lounge"}]
When it should be:
[{"id":"2","name":"Some cool event","org":"Student Senate","location":"HUB 123"}, {"id":"3","name":"Some other cool event","org":"Some club","location":"HUB Lounge"}]
It looks like, since all three tables have a 'name' column, the last column to be selected is overwriting the ones before it. Obviously this is not what I want.
Is there a way to give the column a new name in the querybuilder similar to AS in plain MySQL? Will this even solve my issue? Please provide an example with your answer. I'm not a MySQL expert and I'm a beginner at Laravel. I'm only trying to set up a basic API to use with my actual project.
Thank you!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire