読み込みが終了しない場合は、しばらく待つか、リロードを行なってください。
If loading does not finish, wait for a while or reload.
エンジニア向けの情報を発信するブログです。
どなたでも発信できます。
お好きに利用していただれば幸いです。
private function recursion(Category $category, ?Collection $collection = null)
{
$collection = !$collection
? new Collection()
: $collection;
$collection->push($category);
$children = $category->children;
foreach ($children as $child) {
$collection = $this->recursion($child, $collection);
}
return $collection;
}
public function allChildren(Request $request)
{
$category = Category::query()
->find($request->get('category_id'));
if (!$category instanceof Category) {
abort(404);
}
// ↓↑ 上の関数
$categories = $this->recursion($category);
return view(
'category.index',
[
'parentCategory' => $category,
'categories' => $categories,
]
);
}
@extends('layouts.app')
@section('content')
カテゴリー一覧
<div>
parent: {{isset($parentCategory) ? $parentCategory->name : null}} id: {{$parentCategory->id}}
</div>
<a href="{{route('category.new', ['parent_id' => isset($parentCategory) ? $parentCategory->id : null])}}">新規登録</a>
<div>
@foreach ($categories as $category)
<div style="display: flex;">
<a href="{{route('category', ['category_id' => $category->id])}}">{{$category->name}}</a>
id: {{$category->id}} parent_id: {{$category->parent_id}}
<a href="{{route('category.all_child', ['category_id' => $category->id])}}">
全ての小孫を取得
</a>
</div>
@endforeach
</div>
@endsection