我想使用 bd_products 的主键 (ID) 获取存储在 bd_product_item_ledgers 中的产品数量
bd_products 链接到 bd_product_ledgers 然后,bd_product_ledgers 链接到 bd_product_item_ledgers
表:bd_products
---------------------------------------------------------------------------------
| id | name | description | image | created_at | updated_at
---------------------------------------------------------------------------------
| 1 | shoe | this is a shoe | 15243.jpg |
---------------------------------------------------------------------------------
表:bd_product_ledgers
---------------------------------------------------------------------------------
| id | product_id | cost | created_at | updated_at
---------------------------------------------------------------------------------
| 1 | 1 | 500.00 |
---------------------------------------------------------------------------------
表:bd_product_item_ledgers
---------------------------------------------------------------------------------
| id | product_ledger_id | quantity | created_at | updated_at
---------------------------------------------------------------------------------
| 1 | 1 | 200 |
---------------------------------------------------------------------------------
我可以得到产品的成本,因为那里有 ID,但是我怎样才能得到数量呢?
简单的 MySQL 查询或 Laravel Eloquent 等任何东西都会很好。
谢谢!
最佳答案
创建三个模型 Product、Item、ledgers 并在产品模型和 ledgers 模型中定义您的关系 ledgers,然后您可以通过查询获取数据,例如,
$data = Product::with('ledgers.item')->get();
关于php - 如何使用连接从第 3 级表中获取数据 [Laravel],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55135833/