Building a Laravel Post Project: Adding Categories and Products

In this guide, we will enhance our Laravel post project by adding functionality for categories and products. Follow the steps below to implement this feature step by step.


Step 1: Create Models and Migrations for Category and Product

First, create the models and migrations for categories and products by running the following commands in your terminal:

php artisan make:model Category -m
php artisan make:model Product -m

Next, update the generated migration files with the required database schema.

Migration for categories Table:

Schema::create('categories', function (Blueprint $table) {
    $table->id();
    $table->string('code')->unique();
    $table->string('name');
    $table->timestamps();
});

Migration for products Table:

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('code')->unique()->nullable();
    $table->string('name');
    $table->integer('quantity');
    $table->integer('quantity_alert');
    $table->string('unit');
    $table->integer('cost');
    $table->integer('price');
    $table->integer('tax')->nullable();
    $table->tinyInteger('tax_type')->nullable();
    $table->string('note')->nullable();
    $table->foreignId('category_id')->constrained('categories')->restrictOnDelete();
    $table->timestamps();
});

After defining the schema, run the following command to migrate the tables into your database:

php artisan migrate

Step 2: Generate Filament Resources

Filament is a powerful tool for building admin panels. Use the following commands to generate resources for Category and Product:

php artisan make:filament-resource Category --generate --view
php artisan make:filament-resource Product --generate --view

Step 3: Customize the Category Resource

Open the CategoryResource.php file located in the App\Filament\Resources\CategoryResource directory and update the form method to include the following schema:

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Forms\Components\TextInput::make('code')
                ->default(fn() => "CA_" . str_pad(Category::query()->count('*') + 1, 2, "0", STR_PAD_LEFT))
                ->required()
                ->maxLength(255),
            Forms\Components\TextInput::make('name')
                ->required()
                ->maxLength(255),
        ]);
}

This configuration ensures that:

  1. The code field is auto-generated with a unique prefix (e.g., CA_01, CA_02).

  2. Both the code and name fields are required.


Step 4: Customize the Product Resource

Similar to the category resource, customize the ProductResource.php file as needed to include forms, tables, and any additional functionality. Here’s a basic form schema example for the product resource:

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Forms\Components\TextInput::make('code')->nullable()->maxLength(255),
            Forms\Components\TextInput::make('name')->required()->maxLength(255),
            Forms\Components\TextInput::make('quantity')->numeric()->required(),
            Forms\Components\TextInput::make('unit')->required(),
            Forms\Components\TextInput::make('cost')->numeric()->required(),
            Forms\Components\TextInput::make('price')->numeric()->required(),
            Forms\Components\TextInput::make('tax')->numeric()->nullable(),
            Forms\Components\Select::make('tax_type')
                ->options([
                    1 => 'Inclusive',
                    2 => 'Exclusive',
                ])
                ->nullable(),
            Forms\Components\Textarea::make('note')->nullable(),
            Forms\Components\BelongsToSelect::make('category_id')
                ->relationship('category', 'name')
                ->required(),
        ]);
}

Step 5: Test Your Implementation

Start your development server using the following command:

php artisan serve

Navigate to your Filament admin panel (usually located at /admin) to verify that the category and product resources are working as expected. You should be able to create, edit, and delete categories and products seamlessly.


With these steps, you’ve successfully added categories and products to your Laravel post project. Customize further as per your requirements!