- Shop->orders relationship is possible if orders table has prod_id foreign key.
shops: id, name,... products: id, shop_id,... orders: id, prod_id,....
Then you can build relationship like this.
/Add this in Shop Model:/
public function products()
{
return $this->hasMany('App\Product');
}
public function orders()
{
return $this->hasManyThrough('App\Order', 'App\Product');
}
/Add this in Product Model:/
public function shop()
{
return $this->belongsTo('App\Product');
}
public function orders()
{
return $this->hasMany('App\Order');
}
/Add this in Order Model:/
public function product()
{
return $this->belongsTo('App\Product');
}
Now you can retrieve results with the following queries.
Products belong to Shop:
$shop_products = App\Shop::find(id)->products;
Orders belong to Shop:
$shop_orders = App\Shop::find(id)->products->orders;
Orders belong to product;
$product_orders = App\Product::find(id)->orders;
- You have to create another method for getting order details. Laravel Eloquent doesn't support hasManyThrough relationships if multiple intermediate tables are present. In such cases you have to write joins like this using DB Facade.
$shop_order_details = DB::table('order_details')
->join('orders', 'order_details.order_id', '=', 'orders.id')
->join('products', 'orders.product_id', '=', 'products.id')
->join('shops', 'products.shop_id', '=', 'shops.id')
->select('order_details.*')
->where('shop_id', '=', $id)
->get();
This will give you all order details related to one shop. If you wish to use only eloquent then you have to add shop_id foreign key to your order_details or orders table and follow the above procedure.