[英]Query for relational model in laravel 5.3
我有3個名為“用戶”,“訂單”,“ Orders_products”的模型。 關系如下:
這是模型的代碼
用戶模型
public function order(){
return $this->hasMany('App\Order');
}
訂單模型
public function order_product(){
return $this-> hasMany('App\Orders_product');
}
public function user(){
return $this-> belongsTo('App\User');
}
訂單產品模型
public function order(){
return $this->belongsTo('App\Order');
}
一個用戶可以有多個訂單,所有訂單都可以有多個訂購的產品。 如何獲得特定用戶的所有訂單以及訂購的產品。
用戶模型
public function order()
{
return $this->hasMany(Order::class);
}
訂單模型
public function user()
{
return $this->hasOne(User::class);
}
public function orders_products()
{
return $this->belongsToMany(Orders_products::class);
}
訂單_產品
public function order()
{
return $this->belongsToMany(Order::class);
}
這應該可行,$ userid是您在應用程序中指定用戶ID的方式。
User::with('order_product.order')->where('id', $usersid)->get();
$Order = Order::with('order_product')->where(['user_id'=>$id])->get();
其中$id
是user_id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.