i can get my category and its infinit sub category but i dont know how to show them in a slect option tag in this state: (i just want to show a subcategory (and its infinit sub category) belong to its parent in a blade)
categories
cloth
-Men
--Tshirt
-Women
--Tshirt
here is my Category Model Method code
public function subcategories()
{
return $this->hasMany(Category::class, 'parent_id');
}
here is my migrations
$table->id();
$table->string('title');
$table->string('slug');
$table->foreignId('parent_id')
->nullable()
->constrained('categories')
->cascadeOnDelete()
->cascadeOnUpdate();
here is my index.blade.php view
<select
class="form-select @error('parent') is-ivalid @enderror"
required="">
<option selected="" value="0">None</option>
@foreach($categories as $category)
@if($category->{'parent_id'} === null)
<option
value="{{$category->{'id'} }}">
{{$category->{'title'} }}
</option>
@if(count($category->subcategories ))
@include('admin.layouts.partials.subcategory' , ['sub'=>$category->subcategories])
@endif
@endif
@endforeach
</select>
here is my partials subcategory blade
@foreach($sub as $child)
<option value="{{$child->{'id'} }}">
{{$child->{'title'} }}
</option>
@if(count($child->subcategories))
@include('admin.layouts.partials.subcategory' , ['sub'=>$child->subcategories])
@endif
@endforeach