Alan Cordova Alan Cordova - 29 days ago 12
PHP Question

Multiple Joins Same name Laravel

I have a question i have 4 tables


I need to obtain the img_src_list what is in the List_Galleries but from deals.

I have this var in this time

$query = ListsDeals::where( $matchDeals )
->where('stock', '>', 0)
->orwhere( $matchOtherDeals )
->whereDate('end_date', '>', date('Y-m-d'))
->limit( 4 )
->offset( 0 )
->orderBy( 'start_date' );

$deals = $query->join( 'list_has_deals', '', '=', 'list_has_deals.deal_id' )
->join( 'lists', 'list_has_deals.list_id', '=', '' )
->join( 'list_has_gallery', '', '=', 'list_has_gallery.list_id' )
->join( 'lists_galleries', 'list_has_gallery.gallery_id', '=', '' )->get();

This Give me the results but how i have the same name in all tables for example title for deals and lists that overwrite the names and dont get the values correct, how i can obtain certain columns without overwrite the others? regards.

Answer Source

You can use select() to choose only those columns you want, and use aliases to rename any that clash:

$query->select(['lists.title AS title', ' AS name', ' AS id'])->get();

That way you can cherry-pick only those columns you need and deal with clashes easily.