douzi2749 2018-11-09 20:10
浏览 471
已采纳

Laravel - 制作HasManyThrough关系提供了一个独特的集合

I am having an issue getting a hasManyThrough to work:

public function deliveryContainers() : HasManyThrough
{
    return $this->hasManyThrough(
        DeliveryContainer::class, // Final
        StockMovement::class, // Intermediate
        'product_id', // Foreign key on Intermediate
        'id', // Foreign key on Final
        'product_id', // Local key on Current
        'location_id' // Local key on Intermediate
    )->where('delivery_id', $this->delivery_id);
}

Because the stockMovements table returns multiple results, my resulting delivery containers collection contains duplicate entries. If I could somehow put a group/unique on the intermediate table query then this would be resolved.

I can get a collection with the correct deliveryContainers eager loaded using the following:

public function deliveryContainers()
{
    return $this->hasMany(StockMovement::class, 'entity_id', 'delivery_id')
        ->with('deliveryContainer')
        ->where('product_id', $this->product_id)
        ->get()
        ->unique('location_id');
}

However, to access the deliveryContainer I now have the following:

foreach($this->deliveryContainers() as $row){
    $row->deliveryContainer->id;    
}

And what I would like to have...

foreach($this->deliveryContainers() as $row){
    $row->id;
}

Is there any way to push the eager loaded relationship up a level (if that can be used to describe it), or even better add some kind of unique filter to the hasManyThrough relationship?

Table Structure

delivery_exceptions (where this relationship originates)

  • product_id
  • delivery_id

delivery_containers

  • id
  • delivery_id

stock_movements

  • entity_id (linked to delivery id)
  • product_id

Relationships

  • A delivery exception belongsTo a product
  • A product hasMany stock_movements
  • A stock movement belongsTo a delivery container
  • A delivery exception hasMany delivery containers... (indirectly, through a combination of the product and the stock movements)
  • 写回答

2条回答 默认 最新

  • duanaoou4105 2018-11-09 21:48
    关注

    You've got a really tough setup there and I'm not entirely sure that I got the full idea behind it (also because of you using entity_id at some place instead of delivery_id). But nonetheless, I gave it a shot.

    The hasManyThrough relationship you defined looks actually not too bad, but in my opinion there is a better way to get to the result. But first let's have a look at your relationships:

                                                   3
                                +-------------------------------------+
                   4            v                                     |
            +-------------> Delivery <----------+                     |
            |                                   | 1                   |
            +                                   +                     +
    DeliveryException +---> Product <---+ StockMovement +---> DeliveryContainer
            +                                                         ^
            +---------------------------------------------------------+
                                          2
    

    As a StockMovement already belongs to a DeliveryContainer, which in return belongs to a Delivery, the relation from StockMovement to Delivery (marked as 1) seems obsolete to me. Anyway, to get relation 2 on your model, you can use the paths 3 and 4 to your advantage:

    class DeliveryException
    {
        public function deliveryContainers(): HasMany
        {
            return $this->hasMany(DeliveryContainer::class, 'delivery_id', 'delivery_id');
        }
    }
    

    Obviously, this will give you all the DeliveryContainers, unfiltered by the Product. Therefore I suggest adding a second function:

    public function deliveryContainersByProduct(): HasMany
    {
        return $this->deliveryContainers()
            ->whereHas('stockMovements', function ($query) {
                 $query->where('product_id', $this->product_id);
            });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP