【问题标题】:Joining multiple tables to get an collection of stock items连接多个表以获取库存项目的集合
【发布时间】:2016-03-16 22:40:33
【问题描述】:

我目前正在尝试加入 5 个表以检索已订购商品的列表。是否可以将所有项目放在一起,而不是拥有多个具有不同 ItemID 的“ordered_pa​​cks”集合?

我的代码

if(!\App\OrderedPack::where('orderID', '=', $orderID)->get()->isEmpty()) {
            $packs = \App\OrderedPack::where('ordered_packs.orderID', '=', $orderID)
                ->join('packs', 'ordered_packs.packID', '=', 'packs.packID')
                ->join('pack_items', 'packs.packID', '=', 'pack_items.packID')
                ->join('items', 'pack_items.itemID', '=', 'items.itemID')->get();

我的返回结果

Collection {#288 ▼
  #items: array:2 [▼
    0 => OrderedPack {#289 ▼
      #primaryKey: "ordered_packID"
      #table: "ordered_packs"
      #connection: null
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:30 [▼
        "ordered_packID" => 1
        "orderID" => 80
        "packID" => 7
        "pack_quantity" => 12
        "created_at" => "2016-02-11 14:20:29"
        "updated_at" => "2016-03-08 16:21:02"
        "pack_name" => "niceee"
        "pack_description" => ""
        "pack_itemID" => 5
        "itemID" => 9
        "item_code" => "INT T-C 12/15"
        "companyID" => 1
        "item_name" => "International Terms and Conditions"
        "initial_level" => 5000
        "current_level" => 4000
        "item_typeID" => 6
        "productID" => 1
        "image_location" => "9.pdf"
        "box_total" => 100
        "offsite_level" => 1000
        "locationID" => 1
        "language" => "English"
        "download_url" => ""
        "archived" => 0
        "low_stock_level" => 1000
        "change_description" => ""
        "old_itemID" => 0
        "pdf_only" => 0
        "total_stock" => 5000
        "groupID" => 2
      ]
      #original: array:30 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #fillable: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => OrderedPack {#290 ▼
      #primaryKey: "ordered_packID"
      #table: "ordered_packs"
      #connection: null
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:30 [▼
        "ordered_packID" => 1
        "orderID" => 80
        "packID" => 7
        "pack_quantity" => 12
        "created_at" => "2016-02-11 14:18:35"
        "updated_at" => "2016-03-08 16:21:02"
        "pack_name" => "niceee"
        "pack_description" => ""
        "pack_itemID" => 6
        "itemID" => 7
        "item_code" => "GW CHO INT BRO 12/15"
        "companyID" => 1
        "item_name" => "GW Choice International Brochure (Including Details Guide) 12/15"
        "initial_level" => 5000
        "current_level" => 234
        "item_typeID" => 1
        "productID" => 2
        "image_location" => "7.pdf"
        "box_total" => 100
        "offsite_level" => 2304
        "locationID" => 1
        "language" => "English"
        "download_url" => ""
        "archived" => 0
        "low_stock_level" => 1000
        "change_description" => ""
        "old_itemID" => 0
        "pdf_only" => 0
        "total_stock" => 2538
        "groupID" => 2
      ]

我对包中的每个项目都有一个 orderedpack 实例。我想要一个包含多个项目的有序包实例。

桌子

order->ordered_pa​​cks->packs->pack_items->items

【问题讨论】:

  • 你需要提取什么?物品的集合,仅此而已?
  • 一旦我有了收藏品,我将显示已订购包的列表以及包中的物品列表。只是好奇我是否可以通过一个查询来做到这一点。
  • 因此您需要一个包含相关商品的订单集合。我会用急切的加载给你答案

标签: mysql laravel eloquent laravel-5.1


【解决方案1】:

无法在一个查询中利用 Laravel,但您可以利用 eager loading

$packs = \App\OrderedPack::where('ordered_packs.orderID', '=', $orderID)
->with('packs.pack_items.items')
->get();

假设您的关系设置正确

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 2015-10-28
    • 1970-01-01
    • 2012-02-19
    • 2016-02-01
    • 2016-05-16
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    相关资源
    最近更新 更多