Courses

Filament 3 From Scratch: Practical Course

Generate Simple Resources and hasMany Count

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Creating "simple resources
- Using --generate flag for auto-field detection
- Managing toggleable columns
- Displaying relationship counts in tables

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...

The full lesson is only for Premium Members.
Want to access all 24 lessons of this course? (101 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord