Код: Выделить всё
public static function tree() {
return Category::with(implode('.', array_fill(0, 100, 'children')))->where('parent_id', '=', NULL)->get();
}
Код: Выделить всё
$categories = Category::tree();Код: Выделить всё
@foreach($categories as $index => $category)
{{ $category->title }}
@foreach($category['children'] as $child)
{{ $child->title }}
@endforeach
@endforeach
Я пробовал так:
Код: Выделить всё
public static function tree() {
return Category::with([implode('.', array_fill(0, 100, 'children')) => function($query) {
$query->orderBy('title', 'ASC');
}])->where('parent_id', '=', NULL)->get();
}
Но не сортирует их. У кого-нибудь есть идеи?
Спасибо.
--РЕДАКТИРОВАТЬ--
Следуя предложению Тима о лучше, теперь у меня есть:
Код: Выделить всё
public function children() {
return $this->hasMany('App\Category', 'parent_id', 'id');
}
public function childrenCategories()
{
return $this->children()->with('childrenCategories');
}
Код: Выделить всё
$categories = Category::with(['childrenCategories' =>
function($query) {
$query->orderBy('title', 'ASC');
}])->where('parent_id', '=', NULL)->get();
Подробнее здесь: https://stackoverflow.com/questions/634 ... r-children
Мобильная версия