skip to Main Content

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


  1. First you have to define the fields for the form (modal), then set the parent category and save the data.
    Something like this:

            ->actions([
                TablesActionsEditAction::make(),
                TablesActionsAction::make('subcategory')
                    ->label('Add subcategory')
                    ->form([
    
                        FormsComponentsTextInput::make('name')
                            ->required()
                            ->label('Category')
    
                            // ...
    
                    ])->action(function (Category $record, $data) {
    
                        $data['parent_id'] = $record->id;
                        static::getModel()::create($data);
    
                    }),
    

    Don’t forget to add relationships in the category:

    Category:

    public function parent(): BelongsTo
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }
    
    public function subcategories(): HasMany
    {
        return $this->hasMany(Category::class, 'parent_id');
    }
    
    Login or Signup to reply.
  2. Category Resource:

    public static function form(Form $form): Form
            {
                return $form
                ->schema([
                    FormsComponentsTextInput::make('name')
                        ->label('Category')
                        ->required(),
                    FormsComponentsSelect::make('parent_id')
                        ->relationship('parent', 'name')
                        ->label('Sub Category')
                        ->required()
                        ->createOptionForm([
                            FormsComponentsTextInput::make('name')
                                ->label('Category')
                                ->required()
                        ])
                ]);
            }
    

    Category Model:

    public function parent(): BelongsTo
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }
    

    You can attach parent category or add new one

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search