giacomomasseron/laravel-models-generator
Installation
composer require giacomomasseron/laravel-models-generator
Publish the config file (if needed):
php artisan vendor:publish --provider="GiacomoMasseron\LaravelModelsGenerator\LaravelModelsGeneratorServiceProvider"
First Use Case Generate models for an existing database:
php artisan models:generate
This will scan your database and create model files in app/Models/ with:
Where to Look First
config/models-generator.php (customize paths, naming conventions, or excluded tables).php artisan models:generate (core command) and php artisan models:generate:factory (for factories).app/Models/ for new files and database/factories/ for factories.Generating Models from Scratch
php artisan models:generate --table=users --path=app/Models/Custom
--path).--table) or all tables (default).Generating Factories
php artisan models:generate:factory --table=users
database/factories/ with fakerized data.--count to generate multiple records for seeding.Integration with Migrations
Customizing Generation
config/models-generator.php:
'exclude' => ['migrations', 'failed_jobs'],
snake_case to studly_case or custom patterns:
'naming' => [
'prefix' => 'App\\Models\\',
'suffix' => '',
],
Polymorphic Relationships
user_id, user_type) and creates:
public function user(): MorphTo
{
return $this->morphTo();
}
UUID/ULID Support
*_id and has a type like uuid or char(36), it casts to HasUuid or HasUlid:
protected $casts = [
'id' => Uuid::class,
];
Enum and JSON Casting
enum columns and casts to Enum:
protected $casts = [
'status' => Status::class, // Auto-generated enum class
];
- JSON columns are cast to `array`:
```php
protected $casts = [
'metadata' => 'array',
];
Seeding with Generated Factories
// database/seeders/DatabaseSeeder.php
public function run()
{
\App\Models\User::factory()->count(10)->create();
}
Overwriting Existing Models
--force:
php artisan models:generate --force
--dry-run first to preview changes:
php artisan models:generate --dry-run
Polymorphic Ambiguity
user_id and post_id, the generator may misidentify relationships.morphTo() calls or rename columns.UUID/ULID Misconfiguration
uuid/ulid columns are for primary keys or foreign keys.external_id), it may incorrectly cast to HasUuid.Enum Generation Quirks
enum classes in app/Enums/.string casts or use a polyfill.SQLite-Specific Issues
enum). The generator may fail or produce suboptimal casts.--skip-types to ignore type casting for SQLite.Factory Fakerization
Faker defaults, which may not match your app’s data.->state(fn (array $attributes) => [
'status' => fake()->randomElement(['active', 'inactive']),
])
Verbose Output Enable debug mode to see SQL queries and generation steps:
php artisan models:generate --verbose
Log File
Check storage/logs/laravel.log for errors during generation.
Doctrine DBAL Issues If using unsupported drivers, install the Doctrine DBAL extension:
composer require doctrine/dbal
Custom Model Templates Override the default Blade template by publishing and modifying:
php artisan vendor:publish --tag=models-generator-templates
resources/views/vendor/models-generator/model.stub.Post-Generation Hooks
Use Laravel’s generated:model event to run custom logic after generation:
// app/Providers/EventServiceProvider.php
protected $listen = [
'generated:model' => [
\App\Listeners\PostModelGeneration::class,
],
];
Dynamic Configuration Override config per environment by publishing the config and modifying:
// config/models-generator.php (local override)
'naming' => [
'prefix' => env('MODEL_NAMESPACE', 'App\Models\\'),
],
Skipping Tables via Code Dynamically exclude tables in a service provider:
$generator->exclude(['temp_tables', 'logs']);
# .github/workflows/deploy.yml
- run: php artisan models:generate --force
How can I help you explore Laravel packages today?