Вот как я получаю товары из многоуровневой категории:
Код: Выделить всё
public function childs()
{
return $this->hasMany(self::class, 'parent_id')->whereColumn('id', '!=', 'parent_id');
}
public function descendants()
{
$descendants = [];
foreach ($this->childs as $category)
{
array_push($descendants, $category);
$descendants = array_merge($descendants, $category->descendants());
}
return $descendants;
}
public function treeProducts()
{
$categories = [$this];
$categories = array_merge($categories, $this->descendants());
$ids = [];
forEach($categories as $category)
{
array_push($ids, $category->id);
}
return Product::whereIn('category_id', $ids);
}
Я не могу выполнить этот процесс с помощью Query. Строитель без использования массивов?
Подробнее здесь: https://stackoverflow.com/questions/571 ... l-category
Мобильная версия