2017-02-08 51 views
0

私はLarvel 5.3を使用していますが、私は関係を設定したい4つのテーブルを持っています。laravel関係を設定するには

- Users 
- Orders 

- Products 
- Brands 

オーダーはユーザーに属し、ユーザーは多数のオーダーを持っています。 商品は多くの注文に加えて、1つのブランドに属します。 ブランドには多くの製品があります。

私は製品と製品のブランドでユーザーの注文を得る方法を見つけようとしています。

User.php

public function orders(){ 
    return $this->hasMany('App\Order'); 
} 

Order.php

public function user() { 
    return $this->belongsTo('App\User'); 
} 
public function products(){ 
    return $this->hasMany('App\Product'); 
} 

Product.php

public function orders() { 
    return $this->belongsToMany('App\Orders'); 
} 
public function brand(){ 
    return $this->belongsTo('App\Brand'); 
} 

Brand.php

public function products() { 
    return $this->hasMany('App\Flavour'); 
} 

私は、各注文商品と商品のブランドとともに、ユーザーの注文を含む配列を取りに行きたいと思っています。

誰も助言できますか?

答えて

1

まず、商品と注文には多少の関係があります。

public function products(){ 
    return $this->belongsToMany('App\Product'); 
} 

まずユーザーの注文を取得します。ユーザーには多数の注文があるため、製品リストの表示順序を指定する必要があります。

$order = $user->orders->first(); 

次に、あなたのスーパーアレイのために、すべての製品とそのブランド

foreach($order->products) ... 

を取得し、あなたは

$arr = User::with('orders.products.brand')->get(); //Not tested 
積極的なロードを試すことができます
関連する問題