Laravel 拉雷维尔有许多纵深的地方
我一直在看关于哈斯曼与拉威尔关系的文档,出于某种原因,我在努力解决它,因为文档使它看起来很简单(也许我想得太多了) 目前我有三个模型表:Laravel 拉雷维尔有许多纵深的地方,laravel,has-many-through,relationships,Laravel,Has Many Through,Relationships,我一直在看关于哈斯曼与拉威尔关系的文档,出于某种原因,我在努力解决它,因为文档使它看起来很简单(也许我想得太多了) 目前我有三个模型表: -User (extends authenticatable) id, name, surname, email, password. -Order id, user_id, name, unit, qty -Product (i need to add a q
-User (extends authenticatable)
id,
name,
surname,
email,
password.
-Order
id,
user_id,
name,
unit,
qty
-Product (i need to add a qty table to show how many is ordered).
id,
order_id,
name,
unit,
description,
family
我的产品模型由管理员用于CRUD产品
请有人向我解释一下,我将如何能够显示给管理员哪个用户下了一个订单,以及该订单由什么组成。
我还想向用户显示“订单历史记录”
请问有谁能帮忙吗
更新
-order_product
id,
order_id,
product_id
quantity
产品控制员:
public function orders()
{
return $this->belongsToMany('App\Order')->withPivot('quantity');
}
public function products()
{
return $this->belongsToMany('App\Product')->withPivot('quantity');
}
订单控制员:
public function orders()
{
return $this->belongsToMany('App\Order')->withPivot('quantity');
}
public function products()
{
return $this->belongsToMany('App\Product')->withPivot('quantity');
}
我认为您需要再创建一个类似订单详情的表
-User (extends authenticatable)
id,
name,
surname,
email,
password.
-Order
id,
user_id,
name,
unit,
qty
-Product (i need to add a qty table to show how many is ordered).
id,
name,
unit,
description,
family
-Order_detail(Store product_id, order_id (You can put qty to this table))
id,
order_id,
product_id
要显示多对多关系,您必须创建一个名为 订购带有字段的产品 -身份证 -订单号 -产品标识 -数量 在订单表模型中,将关系定义为
public function products()
{
return $this->belongsToMany('App\Product')->withPivot('quantity');
}
public function orders()
{
return $this->belongsToMany('App\Order')->withPivot('quantity');
}
在产品表模型中将关系定义为
public function products()
{
return $this->belongsToMany('App\Product')->withPivot('quantity');
}
public function orders()
{
return $this->belongsToMany('App\Order')->withPivot('quantity');
}
检索数据
$order= App\Order::find(1);
@foreach ($order->products as $product)
echo $product->pivot->quantity."<br>";
@endforeach
$order=App\order::find(1);
@foreach($order->products as$product)
echo$product->pivot->quantity.“
”;
@endforeach
提及表的字段用户字段:id、姓名、姓氏、电子邮件、密码。订单字段:id、用户id、名称、单位、数量产品字段:id、订单id、名称、单位、说明、家庭其多对多关系。没有多少人通过。您的数量表应命名为order\u product,其中应包含id、product\u id、order\u id、qty等字段,因此我将有一个名为“order\u product”的透视表,起初我是这么认为的,但在我开始之后,我想知道如果没有用户id列,它将如何跟踪下订单的用户?用户id列在产品表中。您可以按用户id对所有订单进行分组。我现在很困惑。我需要按用户id搜索订单,用户id必须包含用户曾经下过的所有订单,如果我单击其中一个订单,必须显示订购产品的列表?请发布相关代码,我能清楚地知道你到现在为止做了什么对不起,我是新来的堆栈溢出,我似乎无法以正确的格式获取我的代码单击下面的“编辑”问题并在末尾提到新代码如何获取数据?提及产出