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

标签 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->包->pack_items->元素

最佳答案

在一个查询中不可能利用 Laravel,但您可以利用 eager loading :

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

假设您的关系设置正确

关于mysql - 连接多个表以获取库存项目的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36035036/

相关文章:

MYSQL即使一张表的计数为0也显示结果

PHP 显示异常行为

laravel - 我该如何创建一个迁移以 Eloquent 地为枚举添加值

php - 无法在访问器方法中获取属性 - laravel Eloquent

MySQL 错误 2006 MySQL 服务器在 Max OS X 10.8.3 上使用 Homebrew 软件升级到 5.6.10 后消失了

php - 使用 PHP 在 mysql 中删除不起作用

mysql - 数据库设计建议

laravel - Laravel Forge 中一个站点的多个域

php - Laravel form::date 函数格式

php - count() 在 laravel 中返​​回软删除项