gpt4 book ai didi

php - 多个连接同名 Laravel

转载 作者:行者123 更新时间:2023-11-29 10:39:13 24 4
gpt4 key购买 nike

我有一个问题,我有 4 张 table

Deals
Lists
Lists_Galleries
List_has_deals
Lists_has_gallery

我需要获取 List_Galleries 中但来自交易的 img_src_list。

我这次有这个变量

$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', 'deals.id', '=', 'list_has_deals.deal_id' )
->join( 'lists', 'list_has_deals.list_id', '=', 'lists.id' )
->join( 'list_has_gallery', 'lists.id', '=', 'list_has_gallery.list_id' )
->join( 'lists_galleries', 'list_has_gallery.gallery_id', '=', 'lists_galleries.id' )->get();

这给了我结果,但是我如何在所有表中具有相同的名称,例如覆盖名称的交易和列表的标题并且没有得到正确的值,我如何获取某些列而不覆盖其他列?问候。

更新

这对于所有结果来说总是相同的。

$deals = $query->join( 'list_has_deals', 'deals.id', '=', 'list_has_deals.deal_id'  )
->join( 'lists', 'list_has_deals.list_id', '=', 'lists.id' )
->join( 'list_has_gallery', 'lists.id', '=', 'list_has_gallery.list_id' )
->join( 'lists_galleries', 'list_has_gallery.gallery_id', '=', 'lists_galleries.id' )
->select([ 'deals.id as deal_id', 'deals.stock as stock', 'deals.price as price', 'deals.price_reduced as price_reduced', 'deals.img_src as img_src', 'deals.title_deal as deal_title' ])->get();

更新

$deals = $query->join( 'list_has_deals', 'deals.id', '=', 'list_has_deals.deal_id'  )
->join( 'lists', 'list_has_deals.list_id', '=', 'lists.id' );

$other = $deals->join( 'list_has_gallery', 'lists.id', '=', 'list_has_gallery.list_id' )
->join( 'lists_galleries', 'list_has_gallery.gallery_id', '=', 'lists_galleries.id' )
->select([ 'deals.id', 'deals.stock', 'deals.price', 'deals.price_reduced', 'deals.img_src', 'deals.title_deal', 'lists_galleries.img_src_list' ])
->addSelect([ 'lists_galleries.img_src_list' ])
->get();

最佳答案

您可以使用 select() 仅选择所需的列,并使用别名重命名任何冲突的列:

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

这样您就可以只挑选您需要的列并轻松处理冲突。

关于php - 多个连接同名 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45847493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com