mercredi 16 mars 2016

Joining multiple tables to get an collection of stock items

I'm currently trying to join 5 tables to retrieve a list of ordered items. Is it possible to have all the items together rather than having multiple collections of 'ordered_packs' with a different ItemID?

My code

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();

my returned result

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
      ]

I have an orderedpack instance for every item in a pack. I would like one ordered pack instance that contains multiple items.

The tables

order->ordered_packs->packs->pack_items->items



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire