jimbolino/laravel-model-builder
Installation Add the package via Composer:
composer require jimbolino/laravel-model-builder:dev-master
Register the service provider in config/app.php:
'providers' => [
// ...
Jimbolino\Laravel\ModelBuilder\ModelBuilderServiceProvider::class,
],
First Run
Add the route to routes/web.php:
Route::get('/generate/models', '\\Jimbolino\\Laravel\\ModelBuilder\\ModelGenerator5@start');
Visit /generate/models in your browser to generate models in storage/models.
Manual Integration
Copy generated models from storage/models to your app/Models directory. Use tools like Beyond Compare or WinMerge to merge changes with existing models.
Reverse-Engineer a Legacy Database
Generate Models Run the generator via the web route or CLI (if extended). Models are created with:
$fillable, $guarded).Manual Refinement
app/Models.Testing Generated Models
public function test_auto_generated_relationships()
{
$user = User::find(1);
$this->assertInstanceOf(User::class, $user);
$this->assertNotNull($user->posts); // hasMany relationship
}
Customizing Generation
Extend the generator by overriding methods in ModelGenerator5:
// Example: Modify how timestamps are handled
public function shouldUseTimestamps($table)
{
return false; // Disable timestamps for all tables
}
Post-Generation Processing
Use Laravel’s model events (created, saved) to add logic after generation:
// app/Providers/AppServiceProvider.php
public function boot()
{
Model::created(function ($model) {
if (str_contains($model->getTable(), 'legacy_')) {
$model->addGlobalScope('legacyScope', function (Builder $query) {
$query->where('active', 1);
});
}
});
}
CI/CD Integration
composer.json to automate model generation and updates:
"scripts": {
"generate:models": "php artisan route:list | grep generate/models && curl http://localhost/generate/models || echo 'Models already up-to-date'"
}
Duplicate Relationship Methods
parent_id and child_id in the same table) generate duplicate method names like parent() and child().Multiple Foreign Keys
public function users()
{
return $this->morphToMany(User::class, 'userable');
}
Timestamp Detection
$timestamps = true or false.shouldUseTimestamps() method or manually adjust the generated models.Soft Deletes
deleted_at) are not always detected accurately.SoftDeletes trait manually or extend the generator to check for the column explicitly.Inspect Generated SQL Enable Laravel’s query logging to debug how the generator reads the database schema:
DB::enableQueryLog();
$generator->generateModels(); // Your generator instance
dd(DB::getQueryLog());
Check Table/Column Whitelisting The generator may skip tables or columns not matching expected patterns (e.g., snake_case). Adjust the regex in the generator if needed.
Handle Reserved Keywords
If your table/column names conflict with PHP/Laravel keywords (e.g., order, group), escape them in the generated model:
protected $table = '`order`';
Custom Model Naming
Override the getModelName() method to enforce naming conventions (e.g., User instead of Users):
protected function getModelName($table)
{
return Str::singular(Str::studly($table));
}
Add Custom Attributes
Extend the generator to include additional attributes like $casts or $dates:
protected function getModelAttributes($table)
{
$attributes = parent::getModelAttributes($table);
$attributes['$casts'] = [
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
return $attributes;
}
Support for Custom Relationships Add logic to detect and generate many-to-many or polymorphic relationships:
public function generateManyToMany($table, $foreignKeys)
{
// Custom logic for pivot tables
}
Exclude Tables/Columns Filter out system tables or sensitive columns by overriding:
protected function shouldSkipTable($table)
{
return in_array($table, ['migrations', 'failed_jobs']);
}
How can I help you explore Laravel packages today?