有3个表 . 第一个是订单:
在示例顺序之后:
class Order extends Model
{
// Table Name
protected $table = 'orders';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
public function user() {
return $this->belongsTo('App\User');
}
public function orderproduct() {
return $this->hasMany('App\OrderProduct');
}
}
第二个是OrderProduct表:
class OrderProduct extends Model
{
// Table Name
protected $table = 'order_product';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
public function order() {
return $this->belongsTo('App\Order');
}
public function product() {
return $this->hasMany('App\Product');
}
}
第三个是产品表:
class Product extends Model
{
// Table Name
protected $table = 'products';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
public function orderproduct() {
return $this->belongsTo('App\OrderProduct');
}
}
我对这种关系不确定 .
我想要做的是:在用户下订单后,如何编写正确的雄辩查询以显示用户订购的产品的订单?我的意思是我在下订单后将用户重定向到他们的订单页面,就在那里我想显示他们的订单详情 .
编辑:我用这个来达到用户ID:auth() - > user() - > id现在使用这个id我可以从第一个表到达order_date . 订单ID是orderproduct表中的外键(order_id) .
从第二张表取数量并使用第二表中的product_id到达产品信息(名称,img,价格...)
所以最后我想显示订单ID订购产品名称订购产品Img订购产品数量订购日期付费货币(数量x价格)