Skip to main content

Generate Simple Resources and hasMany Count

Premium
4:09

Text Version of the Lesson

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 of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

N
Nurbek ✓ Link copied!

рақмет!

HM
Hossam Mohamed ✓ Link copied!

Toggle lessons List new button is not responsive with my Samsung galaxy Note 10 plus, Please fix the design as much as possible. Your site is really great 💗

M
Modestas ✓ Link copied!

Can you explain what exactly is not responsive with it? It seemed to work just fine with different resolutions for us.

M
Modestas ✓ Link copied!

Nevermind! Found the issue and update should be live in a few minutes! Thanks for reporting!

S
steff700 ✓ Link copied!

"doctrine/dbal package ... is pretty accurate but not 100% reliable: it wouldn't handle ENUM fields, for example."

Is it a functional problem or is it only a problem with automatic generation? What is recommended if I have many columns and enum fields?

PK
Povilas Korop ✓ Link copied!

Sorry for a reply much later, but it might be still beneficial to others: in such cases, I would recommend to use the --generate option but then triple-check what it generated and modify the generated values.

LN
Loganathan Natarajan ✓ Link copied!

Very easy to follow. Loved this tutorial.

MN
Md Nishat Miah ✓ Link copied!

Project model has hasMany relations with ProjectImage. I am able to delete images from storage while deleting but unable to delete while update. Can anyone please help me? deleting method in Project model:

static::deleting(function (Project $project) { foreach ($project->projectImages as $image) { Storage::disk('public')->delete($image->url); } $project->projectImages()->delete(); });

MN
Md Nishat Miah ✓ Link copied!

Solved

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.