连接多个表以获取库存项目的集合

时间:2016-03-16 11:57:00

标签: mysql laravel eloquent laravel-5.1

我正在尝试加入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实例。我想要一个包含多个项目的有序包实例。

表格

命令 - > ordered_pa​​cks-> packs-> pack_items->项

1 个答案:

答案 0 :(得分:1)

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

        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    System.out
            .println(ConfigurationWatchListUtil.getConfigurationWatchList(context).getCopyOfFileWatchList().get(0));

假设您的关系设置正确