2 Resources 1 Model

Good evening, I have a Category model and migration. My goal is to have a category and sub categor. Everything is smooth until I want a separate resources for category and sub category. For example inside CategoryResource only have - Image - Name - Status And in inside the SubCategoryResource will have - Image - Name - Category - Ststus Any advice how to achieve this?
Solution:
In CategoryResource, You need to modify the $table by adding ->modifyQueryUsing(fn($query) => $query->whereNull('parent_id')). The same in SubCategoryResource with reversing the condition to only show the sub-categories.
Jump to solution
11 Replies
cheesegrits
cheesegrits14mo ago
Have you come across a problem? You should just be able to create the two resources and specify the $model. I don't know if the Artisan make:filament-resource command will work for the one who's name doesn't match the model, but you could copy and paste files and change names, if not.
MohamedSabil83
MohamedSabil8314mo ago
he can do php artisan make:filament-resource SubCategory then change $model to Category::class inside the SubCategoryResource
cheesegrits
cheesegrits14mo ago
Yup, that's what I meant by "create the two resources and specify the $model", but you made it a lot clearer than I did. 🙂
einnlleinhatt_
einnlleinhatt_OP14mo ago
This is CategoryResouce, the data 'PotongRambut' should not be there because thats a sub-category.
No description
einnlleinhatt_
einnlleinhatt_OP14mo ago
This is the SubCategoryResource using the Category model.
No description
einnlleinhatt_
einnlleinhatt_OP14mo ago
Category Model

class Category extends Model
{
use HasFactory, HasUuids;

protected $table = 'categories';

protected $fillable = [
'name',
'slug',
'parent_id',
'status',
'thumbnail'
];

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

public function subCategories(): hasMany
{
return $this->hasMany(Category::class, 'parent_id');
}
}
Category Model

class Category extends Model
{
use HasFactory, HasUuids;

protected $table = 'categories';

protected $fillable = [
'name',
'slug',
'parent_id',
'status',
'thumbnail'
];

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

public function subCategories(): hasMany
{
return $this->hasMany(Category::class, 'parent_id');
}
}
CategoryResource

public static function form(Form $form): Form
{
return $form
->schema([
Group::make()
->schema([
Section::make()
->schema([
TextInput::make('name')
->required()
->live(onBlur: true)
->afterStateUpdated(function (string $operation, $state, Forms\Set $set)
{
if ($operation !== 'create') {
return;
}

$set('slug', Str::slug($state));
}),

TextInput::make('slug')
->disabled()
->dehydrated()
->required()
->unique(Category::class, 'slug', ignoreRecord: true),
])
]),

Group::make()
->schema([
Section::make()
->schema([
Toggle::make('status')
->helperText('Status kategori aktif atau tidak.')
->default(true),

FileUpload::make('thumbnail')
->label('Feature Image')
->required()
->image()
])
])
]);
}
CategoryResource

public static function form(Form $form): Form
{
return $form
->schema([
Group::make()
->schema([
Section::make()
->schema([
TextInput::make('name')
->required()
->live(onBlur: true)
->afterStateUpdated(function (string $operation, $state, Forms\Set $set)
{
if ($operation !== 'create') {
return;
}

$set('slug', Str::slug($state));
}),

TextInput::make('slug')
->disabled()
->dehydrated()
->required()
->unique(Category::class, 'slug', ignoreRecord: true),
])
]),

Group::make()
->schema([
Section::make()
->schema([
Toggle::make('status')
->helperText('Status kategori aktif atau tidak.')
->default(true),

FileUpload::make('thumbnail')
->label('Feature Image')
->required()
->image()
])
])
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
ImageColumn::make('thumbnail')
->circular(),

TextColumn::make('name')
->color('primary')
->searchable()
->toggleable()
->limit(50),

TextColumn::make('updated_at')
->searchable()
->toggleable()
->since(),

TextColumn::make('created_at')
->searchable()
->toggleable()
->since(),

ToggleColumn::make('status'),
])
}
public static function table(Table $table): Table
{
return $table
->columns([
ImageColumn::make('thumbnail')
->circular(),

TextColumn::make('name')
->color('primary')
->searchable()
->toggleable()
->limit(50),

TextColumn::make('updated_at')
->searchable()
->toggleable()
->since(),

TextColumn::make('created_at')
->searchable()
->toggleable()
->since(),

ToggleColumn::make('status'),
])
}
I added this to the form.

SubCategoryResource

Select::make('parent_id')
->label('Category')
->relationship('mainCategory', 'name', fn (Builder $query) => $query->where('parent_id', null))
->searchable()
->placeholder('Select parent category'),
I added this to the form.

SubCategoryResource

Select::make('parent_id')
->label('Category')
->relationship('mainCategory', 'name', fn (Builder $query) => $query->where('parent_id', null))
->searchable()
->placeholder('Select parent category'),
And this to the table

TextColumn::make('mainCategory.name')
->label('Category')
->searchable()
->sortable(),
And this to the table

TextColumn::make('mainCategory.name')
->label('Category')
->searchable()
->sortable(),
Solution
MohamedSabil83
MohamedSabil8314mo ago
In CategoryResource, You need to modify the $table by adding ->modifyQueryUsing(fn($query) => $query->whereNull('parent_id')). The same in SubCategoryResource with reversing the condition to only show the sub-categories.
einnlleinhatt_
einnlleinhatt_OP14mo ago
Can you give me an example?
MohamedSabil83
MohamedSabil8314mo ago
Sure. CategoryResource
public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn($query) => $query->whereNull('parent_id'))
->columns([
// ...
public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn($query) => $query->whereNull('parent_id'))
->columns([
// ...
SubCategoryResource
public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn($query) => $query->whereNotNull('parent_id'))
->columns([
// ...
public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn($query) => $query->whereNotNull('parent_id'))
->columns([
// ...
einnlleinhatt_
einnlleinhatt_OP14mo ago
Thankyou.
MohamedSabil83
MohamedSabil8314mo ago
You're welcome
Want results from more Discord servers?
Add your server