我正在玩 Laravel 模型,我遇到了一个相当常见的数据库设计模式,我无法为其建立关系(使用 Laravel)。
如果我们只考虑下表(为简单起见):
1.Customers 2.Products 3.Product_Prices
(
产品价格预计会随时间变化,我们需要跟踪)
4.Customer_Orders
5.Customer_Orders_Products (客户可以在一个订单中订购多个产品)
这是我到目前为止所拥有的:
1. Product HAS MANY
Product_Prices
2. Product_Prices BELONGS TO
Products
3. Customer HAS MANY
Customer_Orders
4. Customer_Order BELONGS TO
Customer
5. Customer_Order HAS MANY
Customer_Order_Products
6. Customer_Order_Products BELONGS TO
Customer_Order
我认为Price
for eachCustomer_Order_Products
将通过建立与表的关系来获取Products
?我们如何建立这样的关系?
如何获得Laravel Collection Object
具有以下详细信息的一个(我不确定是否只Eloquent
返回一个集合对象或者它是否与 相同Fluent
):
1. Customer
WITH Customer_Orders
WITH Customer_Orders_Products
WITH Product_Price
?
另外如何设置约束,例如:
1. Customer
WITH Customer_Orders
WITH Customer_Orders_Products
WHERE ?Product_Price
Product_Price < Customers_Order.date_order_placed
谢谢