Laravel 5.7
in Category model:
public function parent()
{
$parent = $this->belongsTo('App\Category', 'parent_id');
return $parent;
}
public function children()
{
return $this->hasMany('App\Category', 'parent_id');
}
public function product()
{
return $this->belongsToMany(Product::class);
}
public function products()
{
return $this->hasMany(Product::class);
}
I created a nested category like this:
Root > Cat1 > cat2 > Cat3
Now I added a product to cat3.
how can I get all products via cat1 or root?
I tried these:
this code works (it return me related product)
if($cat3->products()->count()) // it return true number
For this (its parent):
if($cat2->products()->count()) // it return false number
But for this:
if($cat1->products()->count()) // it always return 0
And this
if($root->products()->count()) // it always return 0
what is my wrong?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire