How can I do the "unlimited" multi-level subcategory on Filament?
This is what I have right now:
Category Table:
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedInteger('parent_id');
$table->timestamps();
});
Category Resource:
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->label('Category')
->required()
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name')
->label('Category'),
])
->actions([
TablesActionsEditAction::make(),
TablesActionsAction::make('subCategory'),
]);
}
How can when user click on the "subCategory" button, the same table will navigate into the subcategory and allow user to add another category to that subcategory?
2
Answers
First you have to define the fields for the form (modal), then set the parent category and save the data.
Something like this:
Don’t forget to add relationships in the category:
Category:
Category Resource:
Category Model:
You can attach parent category or add new one