Search code examples
phpeloquentlaravel-5.3one-to-many

Query for relational model in laravel 5.3


I have 3 models named "User","Order","Orders_products". The relations are following:

  1. User has many Order
  2. Order has many Orders_products

Here are the model's code

User Model

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

Order Model

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

Orders_products Model

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

An user could have multiple orders and all orders could have multiple ordered products. How can i get a particular users all orders along with the ordered products.


Solution

  • $Order = Order::with('order_product')->where(['user_id'=>$id])->get(); 
    

    where $id is the user_id