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:Jump to 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.11 Replies
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.
he can do
php artisan make:filament-resource SubCategory
then change $model to Category::class inside the SubCategoryResourceYup, that's what I meant by "create the two resources and specify the $model", but you made it a lot clearer than I did. 🙂
This is CategoryResouce, the data 'PotongRambut' should not be there because thats a sub-category.
This is the SubCategoryResource using the Category model.
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.Can you give me an example?
Sure.
CategoryResource
SubCategoryResource
Thankyou.
You're welcome