@Snapey
Category.php Model
public function products()
{
return $this->hasMany(Product::class);
}
public function subCategories()
{
return $this->hasMany(SubCategory::class,'id_category');
}
SubCategories.php Model
public function products()
{
return $this->hasMany(Product::class,'id_subcategory');
}
public function category()
{
return $this->belongsTo(Category::class);
}
Product.php Model
public function category()
{
return $this->belongsTo(Category::class);
}
in controller:
$categories = Category::with('subcategories.products')->get();
in view:
<ul>
@foreach($categories as $category)
echo'<li value=""> {{$category->namecategory}}';
<ul>
@foreach($category->subCategories as $subCategory)
echo'<li value="">{{$subCategory->namesubcategory}}';
<ul>
@foreach($subCategory->products as $product)
echo'<li value="">{{$product->namesubling}}</li>';
@endforeach
</ul>
</li>
@endforeach
</ul>
</li>
@endforeach
</ul>
its throwing error Invalid argument supplied for foreach() if i remove 2 foreach then its working
i know that its not taking data as array so its throwing error but how i can do so i can achieve my desire view
mobile and accessory
mobile
iphone
mobile cover
iphone cover
i m try to achieve like this don't know how i do that please help me guys