So, we have a belongsTo
relationship from Product to Category. Now, what if we want to manage Categories? In this 2-in-1 lesson, we will create that second Filament Resource in a simple "modal" way. Also, we will see how to show the number of products for each category in the table.
Simple Resource for Categories
Similarly to the products, we could generate another Filament Resource for the Category
Model:
php artisan make:filament-resource Category
But I want to show you two additional options.
First, not every CRUD needs all the create/edit pages. If it's simply one field of "categories.name", like we have here, we could use modals to manage the information. The UX would be quicker, right?
So:
php artisan make:filament-resource Category --simple
And wait, don't run this command yet. Another trick is incoming.
If you followed the first Resource example, you would think you need to add the "name" field as a table column and as a form field later, right?
But what if Filament could generate it for us, guessing the fields from our database schema?
Let's add another option, --generate
.
php artisan make:filament-resource Category --simple --generate
We refresh the page and see the new menu item "Categories", with the column Name already shown in the table. And I haven't written a single line of code for it!
Here's the Resource code generated...