Skip to main content

First CRUD Menu: Product Resource

Premium
5:04

Text Version of the Lesson

Let's create a menu item and a table/form to manage products.


Generate Filament Resource

Every menu item in Filament is called a Resource, a visual representation of the Eloquent Model. Imagine we have this simple Model with two fields:

app/Models/Product.php:

class Product extends Model
{
use HasFactory;
 
protected $fillable = ['name', 'price'];
}

Then we generate a Filament Resource for this model:

php artisan make:filament-resource Product

It generates a folder app/Filament/Resources/ProductResource with these...

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

CB
Caspar Bisschop ✓ Link copied!

About:

One thing that surprised me personally about Filament: after create/edit form submission, it doesn't redirect you back to the table.

If you remove the edit and create from public static function getPages(): then it will open a modal, so you won't need the redirect.

PK
Povilas Korop ✓ Link copied!

Yes, I'm also planning to later talk about Simple resources where no redirect needed.

M
mathCube ✓ Link copied!

yes that's works

MC
Mark Constable ✓ Link copied!

Late to the party but that was a brilliant revelation!

R
rayncy ✓ Link copied!

one thing I noticed is if you have a custom code on mutateFormDataBeforeCreate function. this wont execute this function if you remove edit and create on getpages() function.

MK
Martynas Ki ✓ Link copied!

amazing tip! :)

ES
Erik Slooff ✓ Link copied!

Good looking contant so far, will probably sign up.

One nice option to the artisan command to make a Resource worth mentioning is this:

php artisan make:filament-resource Customer --generate
PK
Povilas Korop ✓ Link copied!

Yes I showed that in the other lesson a bit later. Great option, indeed!

A
Ariziky ✓ Link copied!

Hi. Is it possible to use Filament table builder using external data like json data. Thanks

ES
Erik Slooff ✓ Link copied!

Yup, the Filament site has a nice article on that topic: https://filamentphp.com/community/how-to-consume-an-external-api-with-filament-tables

A
Ariziky ✓ Link copied!

Thanks

D
dr_capins ✓ Link copied!

I have a problem with a resource: i created the form for the creating/editing of a row, but I need to make also a "bulk creting action": I have to insert in the creation form a start date and a end date, and then for each date in the range I have to create a new row with the other fields value. My problem is: how to create a specific form for this "bulk creation"?

PK
Povilas Korop ✓ Link copied!

I wouldn't call the thing you described as a "bulk action", in Filament terms the "bulk action" means that you select multiple records with checkboxes and then delete (or do something else) in bulk.

In your case, it's a custom action, which I would probably do as a Custom Page which means it's a Livewire component, and then you would add a link to it in your resource.

Example of such custom page with a form is in this article.

L
luiszu7779 ✓ Link copied!

Hello, do you know if I can in some way use the Filament TextInput in a custom blade component ? I would like to keep the same the same design everywhere in the panel. Thx !

PK
Povilas Korop ✓ Link copied!

Filament has a few of their own Blade components, maybe try to use those? Here's my video about it.

M
maminskik ✓ Link copied!

Hey, it is possible to add event listener update last login column(users table) in filament?

M
Modestas ✓ Link copied!

It is!

But you will have to create a custom login page to add the function (You can follow this guide: https://laraveldaily.com/post/filament-registration-form-extra-fields-choose-user-role - just change the register action to login everywhere, same logic!) - then once user is logged in - you can add the column update.

Or another option is to listen for the Login event. This is probably the fastest way to handle this. Here's the event you might want to listen to vendor/laravel/framework/src/Illuminate/Auth/Events/Login.php

M
maminskik ✓ Link copied!
<?php

namespace App\Listeners;

use Illuminate\Auth\Events\Login;

class UpdateLastLoginAt { /** * Create the event listener. */ public function __construct() { // }

/**
* Handle the event.
*/
public function handle(Login $event): void
{
$event->user->last_login_at = now();
$event->user->save();
}

} i have error: Undefined method 'save'.intelephense(1013) function Model::save(array|null $options = []): bool Save the model to the database.

M
Modestas ✓ Link copied!

Re-fetch the user based on the ID, rather than trying to save like this. It should work as you should have an ID accessible

M
maminskik ✓ Link copied!

thx, working

public function handle(Login $event) { $user = User::find($event->user->id);

if ($user) {
$user->last_login_at = now();
$user->save();
}
}
AS
ahmad sawaie ✓ Link copied!

i ahve prblem at edit select users thats the edit not appear which user have edit how can i fix it

DB
Dendy B. Sulistyo ✓ Link copied!

problem when click New Produk button, fill forms push create, and got error : Target class [Spatie\LaravelIgnition\ContextProviders\ComponentRegistry] does not exist.

DB
Dendy B. Sulistyo ✓ Link copied!

it solved, using composer remove --dev spatie/laravel-ignition

I
IRESIS ✓ Link copied!

Hey, how I can prevent double click in filament?

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.