| Install | |
|---|---|
composer require ivanmercedes/laravel-forminertia |
|
| Latest Version: | v1.0.0-beta |
| PHP: | ^8.4 |
A plug-and-play form builder for Laravel’s Inertia Starter Kit — powered by ShadCN UI.
You can install the package via composer:
composer require ivanmercedes/laravel-forminertia
Then run the installation command to publish the default resources:
php artisan forminertia:install
FormInertia can automatically generate form classes based on your database table structure, similar to Filament's generate command:
# Generate form from model/table
php artisan forminertia:generate User
# Generate with custom table name
php artisan forminertia:generate User --table=custom_users
# Exclude specific columns
php artisan forminertia:generate User --exclude=password,remember_token
# Force overwrite existing form
php artisan forminertia:generate User --force
# Custom output path
php artisan forminertia:generate User --path=app/Forms/Custom/UserForm.php
The command will:
Example generated form:
<?php
namespace App\Forms;
use LaravelForminertia\Base\Form;
use LaravelForminertia\Fields\TextField;
use LaravelForminertia\Fields\TextareaField;
use LaravelForminertia\Fields\CheckboxField;
class UserForm extends Form
{
public function schema(): array
{
return [
TextField::make('name')
->label('Name')
->placeholder('Enter name')
->required(),
TextField::make('email')
->label('Email')
->placeholder('Enter email')
->required(),
TextareaField::make('bio')
->label('Bio')
->placeholder('Enter bio')
->rows(4),
CheckboxField::make('is_active')
->label('Is Active')
->default(false),
];
}
}
FormInertia uses the same ShadCN UI components that come with the Laravel + Inertia Starter Kit, including Input, Select, and Checkbox.
However, since the Starter Kit doesn’t include a Textarea component by default, FormInertia automatically installs it during setup to ensure full compatibility out of the box.
Each form is a PHP class that defines its structure (sections, grids, and fields).
<?php
namespace App\Forms;
use LaravelForminertia\Base\Form;
use LaravelForminertia\Base\Grid;
use LaravelForminertia\Base\Section;
use LaravelForminertia\Fields\{TextField, SelectField, CheckboxField, TextareaField};
class UserForm extends Form
{
public function schema(): array
{
return [
Section::make('User Information')
->schema([
Grid::make(2)
->schema([
TextField::make('name')
->label('Full Name')
->placeholder('John Doe')
->required(),
TextField::make('email')
->label('Email Address')
->type('email')
->placeholder('user@example.com')
->required(),
SelectField::make('role')
->label('User Role')
->required()
->options([
'admin' => 'Administrator',
'editor' => 'Editor',
'user' => 'User',
]),
])
]),
Section::make('Preferences')
->schema([
CheckboxField::make('is_active')
->label('Active Account'),
TextareaField::make('bio')
->label('Biography')
->rows(6)
->placeholder('Tell us a bit about this user...'),
]),
];
}
}
The controller injects the schema into your Inertia view.
<?php
namespace App\Http\Controllers;
use App\Forms\UserForm;
use App\Models\User;
use Illuminate\Http\Request;
use Inertia\Inertia;
class UserController extends Controller
{
public function create()
{
// Create form without data (for new records)
$form = UserForm::make();
return Inertia::render('user/create', [
'form' => $form,
]);
}
public function edit(User $user)
{
// Fill form with existing data - now works directly with models!
$form = UserForm::make($user);
return Inertia::render('user/edit', [
'form' => $form,
]);
}
public function editWithDefaults()
{
// Fill form with custom default values
$form = UserForm::make([
'name' => 'John Doe',
'email' => 'john@example.com',
'role' => 'user',
'is_active' => true,
]);
return Inertia::render('user/create', [
'form' => $form,
]);
}
}
import { store } from "@/routes/users";
import FormBuilder, {
FormBuilderProps,
} from "@/components/forminertia/form-builder";
export default function create({
form,
}: {
form: FormBuilderProps["formSchema"];
}) {
return (
<div className="max-w-3xl mx-auto">
<FormBuilder
formSchema={form}
form={store.form()}
submitLabel="Create User"
/>
</div>
);
}
Forminertia makes it incredibly easy to populate forms with existing data.Here are the different ways you can do it:
// Fill form directly with Eloquent models
$user = User::find(1);
$form = UserForm::make($user);
// Fill form with array data
$form = UserForm::make([
'name' => 'John Doe',
'email' => 'john@example.com',
'role' => 'admin',
'is_active' => true,
]);
// Works with Collections too
$userData = collect([
'name' => 'Jane Doe',
'email' => 'jane@example.com',
'role' => 'editor',
]);
$form = UserForm::make($userData);
// Create form first, then fill
$form = UserForm::make()
->fill(['name' => 'Jane Doe', 'email' => 'jane@example.com'])
->build();
// Alternative method for any object with toArray()
$user = User::find(1);
$form = UserForm::make()->fillFromModel($user)->build();
Smart Data Handling: The form automatically detects the data type and converts it appropriately:
->toArray()->toArray()toArray(): Automatically convertedFields will automatically populate with matching data. Fields without matching data will use their default values or remain empty.
The MIT License (MIT). Please see License File for more information.