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? (36 h 00 min)

You also get:

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

Already a member? Login here

Caspar Bisschop avatar

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.

👍 6
Povilas Korop avatar

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

mathCube avatar

yes that's works

Mark Constable avatar

Late to the party but that was a brilliant revelation!

rayncy avatar

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.

Martynas Ki avatar

amazing tip! :)

Erik Slooff avatar

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
👍 4
😍 1
Povilas Korop avatar

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

Ariziky avatar

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

dr_capins avatar

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"?

Povilas Korop avatar

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.

luiszu7779 avatar

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 !

Povilas Korop avatar

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

maminskik avatar

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

Modestas avatar

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

maminskik avatar
<?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.

Modestas avatar

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

maminskik avatar

thx, working

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

if ($user) {
$user->last_login_at = now();
$user->save();
}
}
ahmad sawaie avatar

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

Dendy B. Sulistyo avatar

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

Dendy B. Sulistyo avatar

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

IRESIS avatar

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.