rayvenues/eloquent-model-generator
composer require rayvenues/eloquent-model-generator --dev
users):
php artisan ray:generate:model User
This creates a User model in app/Models with App\Models namespace, auto-detecting the users table.php artisan for the ray:generate:model command and its options.id → $primaryKey, created_at → $timestamps).--table-name or --output-path.Generate a CRUD-friendly model for a posts table with custom output path:
php artisan ray:generate:model Post --table-name=posts --output-path=Custom/Models
This creates Post.php in app/Custom/Models with Custom\Models namespace.
Schema-Driven Generation:
php artisan make:model.Reservation model for a reservations table with timestamps and soft deletes:
php artisan ray:generate:model Reservation --table-name=reservations
Team Onboarding:
--output-path=Domain/Models).Auth/Models directory:
php artisan ray:generate:model User --output-path=Auth/Models --namespace=App\Auth\Models
Integration with Migrations:
php artisan migrate to ensure models are generated for newly created tables.MigrateFresh command) to auto-generate models:
// app/Console/Commands/MigrateFresh.php
public function handle() {
$this->call('migrate:fresh');
$this->call('ray:generate:model', ['name' => 'User']);
}
Partial Overrides:
Product, then add a custom getFormattedPrice() method.Dynamic Table Names:
--table-name to handle pluralization inconsistencies (e.g., role table → Role model):
php artisan ray:generate:model Role --table-name=role
Namespace Organization:
Admin/Models, Api/Models) for better IDE navigation:
php artisan ray:generate:model AdminUser --output-path=Admin/Models --namespace=App\Admin\Models
Custom Field Mappings:
slug → $fillable):
// After generation, manually add:
protected $fillable = ['slug', 'title'];
Testing:
- run: php artisan ray:generate:model User --table-name=users
Table Name Mismatches:
users → User). If your table is user, use --table-name=user to avoid errors.php artisan ray:generate:model User --debug to see the resolved table name.Output Path Permissions:
--output-path points to a restricted directory, the command fails silently. Use absolute paths (e.g., /var/www/app/Models) to avoid issues.mkdir -p app/Custom/Models && chmod -R 775 app/Custom/Models
Namespace Collisions:
User in App\Models and Admin\Models) can cause IDE confusion.AdminUser instead of User).Soft Deletes/Timestamps:
created_at, updated_at, and deleted_at columns but doesn’t add use SoftDeletes or HasFactory traits by default.Foreign Key Handling:
belongsTo). You’ll need to add these manually or extend the package to support --with-relations.Dry Run:
--dry-run to preview changes without writing files:
php artisan ray:generate:model User --dry-run
Verbose Output:
-v or -vv for detailed logs:
php artisan ray:generate:model User -vv
Schema Inspection:
php artisan schema:dump --prune
Custom Field Types:
generateField() method in a service provider:
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->extend('model-generator', function ($generator) {
$generator->addFieldType('json', function ($field) {
return "protected \${$field['name']};";
});
return $generator;
});
}
Pre/Post-Generation Hooks:
registering event to modify generated models:
// app/Providers/EventServiceProvider.php
protected $listen = [
'eloquent.model.generated' => [
'App\Listeners\AddCustomLogicToModel',
],
];
Template Overrides:
php artisan vendor:publish --tag=model-generator-views
resources/views/vendor/model-generator/model.stub to customize the output.Batch Generation:
// app/Console/Commands/GenerateModels.php
public function handle() {
$tables = ['users', 'posts', 'roles'];
foreach ($tables as $table) {
$this->call('ray:generate:model', [
'name' => studly_case($table),
'--table-name' => $table,
]);
}
}
How can I help you explore Laravel packages today?