DEV Community

Cover image for Filament: Generate Resource from Existing DB Schema
Sergio Peris
Sergio Peris

Posted on • Originally published at sertxu.dev

1

Filament: Generate Resource from Existing DB Schema

In Filament, you can generate a Filament Resource with the make:filament-resource Artisan command.

This command will generate an empty resource, that you can configure it as you like.

If you already have the migration and model done in your Laravel app, you can use the parameter --generate in order to let Filament try to guess the fields from your database.

php artisan make:filament-resource Model --generate
Enter fullscreen mode Exit fullscreen mode

For example, if you have a Tag model with a name and description fields, running the following command:

php artisan make:filament-resource Tag --generate
Enter fullscreen mode Exit fullscreen mode

Will generate the following resource:

class CategoryResource extends Resource
{
    // ...

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->required()
                    ->maxLength(255),

                Forms\Components\TextInput::make('description')
                    ->required()
                    ->maxLength(255),
            ]);
    }

    // ...
}
Enter fullscreen mode Exit fullscreen mode

You should keep in mind the generation will not always be accurate, so check the code and make any necessary changes to it.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay