I have tables as below :
store
id
name
category
id
parent_id
name
product
id
name
category_product
product_id
category_id
store_category
store_id
category_id
store_product
store_id
product_id
Models :
Store.php
public function category()
{
return $this->belongsToMany(Category::class,'store_category');
}
public function product()
{
return $this->belongsToMany(Proeuct::class,'store_product');
}
public function storeCategoryProducts()
{
return $this->category()->with('products');
}
Category.php
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id');
}
public function children()
{
return $this->hasMany(Category::class, 'parent_id', 'id');
}
public function products()
{
return $this->belongsToMany(Product::class,'category_product','category_id','product_id');
}
public function store()
{
return $this->belongsToMany(Store::class,'store_category','category_id','store_id');
}
Product.php
public function Category()
{
return $this->belongsToMany(Category::class, 'category_product', 'product_id',
'category_id');
}
public function Store()
{
return $this->belongsToMany(Store::class,'store_product','product_id','store_id');
}
Now, i want to fetch all data by store id as below format :
store
relation => Parent_category
relation=> child_category
relation=>product
What i have tried :
Store::with('storeCategoryProducts')->where('store_id',$storeId)->first()
I am getting the data as in the format i need but getting products of all stores, but i need the product of the specific store that are related to $storeId
Is there any other way to do this with eloquent .
Any suggestion are appreciated.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire