user11001/eloquent-model-generator
Installation
composer require --dev pepijnolivier/eloquent-model-generator
Add the service provider to config/app.php (if not auto-discovered):
'providers' => [
// ...
PepijnOlivier\EloquentModelGenerator\EloquentModelGeneratorServiceProvider::class,
],
Publish Config (Optional)
php artisan vendor:publish --provider="PepijnOlivier\EloquentModelGenerator\EloquentModelGeneratorServiceProvider" --tag="config"
Configure paths, naming conventions, and generators in config/eloquent-model-generator.php.
First Generation
php artisan model:generate
This generates models for all tables in your database. Run with --table=users to target a specific table.
config/eloquent-model-generator.php – Customize naming, paths, and generator behavior.model:generate – Generate models for all tables.model:generate:table – Generate a model for a specific table.model:generate:relation – Generate relations for an existing model.config/eloquent-model-generator.php defines where custom generators are stored (default: app/Generators).Add a Table to Your Database
Create a posts table with columns like id, title, body, and user_id.
Generate the Model
php artisan model:generate:table posts
This creates:
app/Models/Post.php (with fillable fields, casts, and timestamps).user() relation method (if user_id exists and references users table).Use the Model
use App\Models\Post;
$posts = Post::with('user')->get(); // Automatically resolves the relation.
php artisan model:generate
Generates models for all tables, including relations (e.g., belongsTo, hasMany).php artisan model:generate:table orders --relations
Generates only the orders model with relations.status to posts), regenerate the model:
php artisan model:generate:table posts
The generator detects new columns and updates the model.app/Generators/CustomPostGenerator.php) extending PepijnOlivier\EloquentModelGenerator\Generators\ModelGenerator.
public function generateRelation($relationName, $foreignKey, $localKey, $model)
{
if ($relationName === 'user') {
return $model->belongsTo(User::class, 'user_id', 'id')->withDefault();
}
return parent::generateRelation($relationName, $foreignKey, $localKey, $model);
}
'generators' => [
'App\\Generators\\CustomPostGenerator',
],
imageable_id + imageable_type). Customize via config:
'polymorphic' => [
'columns' => ['imageable_id', 'imageable_type'],
],
post_tag), generate models with belongsToMany:
php artisan model:generate:table post_tag
Then manually define the relation in the parent model (e.g., Post.php):
public function tags()
{
return $this->belongsToMany(Tag::class, 'post_tag');
}
app/Providers/AppServiceProvider:
Post::observe(PostObserver::class);
laravel-shift/api-resource-generator.Version Control
.gitignore):
/app/Models/*
post-generate script to commit changes:
php artisan model:generate && git add app/Models && git commit -m "chore: update models"
Testing
$this->partialMock(PepijnOlivier\EloquentModelGenerator\EloquentModelGenerator::class, ['generateModel']);
$this->artisan('model:generate:table', ['table' => 'posts'])->assertExitCode(0);
CI/CD
# .github/workflows/generate-models.yml
jobs:
generate-models:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-php@v3
- run: composer install
- run: php artisan model:generate
- run: git diff --exit-code
Overwriting Existing Models
--dry-run to preview changes:
php artisan model:generate --dry-run
--backup (if supported in future versions).Circular Relations
User has Post, Post has User), the generator may produce ambiguous relation names. Resolve by:
'relations' => [
'user' => 'author',
],
Reserved Keywords
class, table, or created_at may cause syntax errors. Rename them in the database or use config to ignore:
'ignored_columns' => ['class', 'table'],
Soft Deletes
deleted_at columns but doesn’t enable soft deletes by default. Add this to your model:
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
Custom Primary Keys
uuid), the generator may not handle it correctly. Override the primary key in config:
'primary_key' => 'uuid',
Verbose Output Enable debug mode in config:
'debug' => true,
Or run with:
php artisan model:generate --verbose
Log Generation
Check storage/logs/laravel.log for errors during generation. Example log entry:
[2025-11-13 12:00:00] local.INFO: Generating model for table [posts]...
[2025-11-13 12:00:01] local.ERROR: Failed to generate relation [user]: Column [user_id] not found.
Manual Relation Fixes If relations are misgenerated, manually correct them in the model:
// Wrong (generated):
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'user_id');
}
// Correct:
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
Naming Conventions Customize model/class naming in config:
'naming' => [
'model' => 'PostModel', // Default: Post
'class' => 'App\\Models\\PostModel',
],
Excluding Tables
Ignore specific tables (e.g., migrations, failed_jobs):
'ignored_tables' => ['
How can I help you explore Laravel packages today?