i want to show categories list on selectbox with tree structure (multi level subcategory)
similar this:
-electronic
--camera
---sumsung
---- other subcategory
---- ...
---lg
-art
table structure:
+----+------------+-----------+
| id | name | parent_id |
+----+------------+-----------+
| 1 | electronic | 0 |
+----+------------+-----------+
| 2 | arts | 0 |
+----+------------+-----------+
| 3 | camera | 1 |
+----+------------+-----------+
| 4 | sumsung | 3 |
+----+------------+-----------+
| 4 | lg | 3 |
+----+------------+-----------+
Category model:
public function children()
{
return $this->hasMany(self::class, 'parent_id', 'id');
}
public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
}
and this return only category and 1 level subcategories
<select>
@foreach($data as $categories)
<optgroup label="{{ $categories->name }}">
@foreach($categories->children as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</optgroup>
@endforeach
</select>
how to show multi level subcategory?