I have the following models and relations
class Advert extends Model
{
public function category() {
return $this->belongsTo('App\Category');
}
}
class Category extends Model
{
public function parent() {
return $this->belongsTo('App\Category', 'category_id','id');
}
public function children() {
return $this->hasMany('App\Category', 'category_id');
}
public function adverts() {
return $this->hasMany('App\Advert');
}
}
I have table with adverts who belong to a category ... The advert's category can be a subcategory of another category ... Subcategory level is max 4. How can i retrieve all the adverts from all subcategories through the main root category it belongs.
For example
<ul>
<li>Smartphones
<ul>
<li>Android
<ul>
<li>Samsung</li>
<li>Huawei</li>
<li>LG</li>
<li>Meizu</li>
<li>Acer</li>
</ul>
</li>
<li>Apple
<ul>
<li>iPhone 5</li>
<li>iPhone 6</li>
<li>iPhone X</li>
</ul>
</li>
</ul>
</li>
</ul>
How can i get all adverts from all subcategories for example if i click on Smartphones ... Do i have to iterate through each level or there is an easier solution with relations...
Thanks