N
N
Nikita Dergachov2017-07-31 14:51:34
Laravel
Nikita Dergachov, 2017-07-31 14:51:34

How to get fields with the same name but in different tables when executing a join query?

There are 2 tables booking_restaurants
: id, id_restaurant, type
restaurant: id, name, type

DB::table('booking_restaurants')
                            ->join('restaurants', 'booking_restaurants.id_restaurant', '=', 'restaurants.id')
                            ->get();

In these tables, the type fields match and as a result I get the value from only one table, how can I get rid of this and get the booking_restaurants type and restaurants type?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor, 2017-08-01
@vanillathunder

In this case, you need to specify the full path to the columns and aliases, by default all columns (*) are selected, which leads to overwriting.

->select(["booking_restaurants.type", "restaurants.type as restaurants_type"])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question