Installation:
composer require abevanation/reversify
php artisan vendor:publish --tag=reversify-config
Review the published config at config/reversify.php.
First Use Case: Reverse-engineer an existing database table into Laravel components:
php artisan reversify:generate --table=users
This generates:
database/migrations/.app/Models/.app/Http/Controllers/.Where to Look First:
config/reversify.php (customize naming conventions, paths, and defaults).reversify:generate (core command).reversify:model (generate only models).reversify:controller (generate only controllers).tests/ in the package repo for sample outputs.Batch Generation: Generate components for multiple tables at once:
php artisan reversify:generate --tables=users,posts,comments
Customizing Output: Override defaults via config or command options:
php artisan reversify:generate --table=users --model-path=App/Entities --controller-path=App/Http/Resources
Integrating with Existing Code:
app/Models/User.php:
class User extends \GeneratedUser {
public function customScope() { ... }
}
class UserController extends \GeneratedUserController {
public function customMethod() { ... }
}
Relationship Handling: Reversify auto-detects foreign keys and generates relationships. Manually adjust in the model:
public function posts() {
return $this->hasMany(Post::class);
}
Traits and Attributes:
Use --with-traits and --with-attributes flags to include common traits (e.g., HasUuids, SoftDeletes) or attributes (e.g., fillable, casts):
php artisan reversify:generate --table=users --with-traits=SoftDeletes --with-attributes="fillable:email,password"
Legacy System Migration:
php artisan reversify:generate --all-tables
Rapid Prototyping:
php artisan reversify:generate --table=products --controller=yes
API Development:
hidden, appends):
php artisan reversify:generate --table=users --with-attributes="hidden:password,remember_token"
Database Connection:
Ensure the reversify.connection config points to your target database:
'connection' => env('DB_CONNECTION', 'mysql'),
Naming Conventions: Customize table-to-model naming in config:
'naming' => [
'model_suffix' => '',
'controller_suffix' => 'Controller',
],
Seeding: Use generated models in seeders:
use App\Models\User;
User::factory()->count(10)->create();
Testing: Test generated migrations by running:
php artisan migrate
Foreign Key Ambiguity:
Reserved Keywords:
order, group) may cause syntax errors.--prefix flag or rename in the migration:
Schema::rename('order', 'orders');
Circular Dependencies:
users and roles) may require manual ordering.--skip-relationships.Configuration Overrides:
php artisan reversify:generate --table=users --model-path=CustomPath
Soft Deletes:
SoftDeletes may conflict with existing deleted_at columns.'traits' => [
'SoftDeletes' => false,
],
Verbose Output: Enable debug mode in config:
'debug' => true,
Or use the --verbose flag:
php artisan reversify:generate --table=users --verbose
Dry Runs: Preview changes without generating files:
php artisan reversify:generate --table=users --dry-run
Logging:
Check storage/logs/laravel.log for errors during generation.
Partial Generation: Generate only migrations or models separately:
php artisan reversify:model --table=users
php artisan reversify:migration --table=users
Custom Templates:
Override Blade templates for models/controllers in resources/views/vendor/reversify/:
vendor/abevanation/reversify/resources/views/ to your project.Excluding Tables: Skip system tables in config:
'excluded_tables' => ['migrations', 'failed_jobs'],
Post-Generation Hooks: Use Laravel events to extend generated files:
// In EventServiceProvider
protected $listen = [
'reversify.generated' => [
\App\Listeners\PostGenerateModel::class,
],
];
5. **Performance**:
For large schemas, generate tables in batches to avoid memory issues:
```bash
php artisan reversify:generate --tables="table1,table2" --batch-size=5
Testing Generated Code: Add tests for generated components:
// tests/Feature/Models/UserTest.php
public function test_user_model() {
$user = new \App\Models\User();
$this->assertTrue(method_exists($user, 'posts'));
}
Version Control: Commit generated files to version control, but document them as "auto-generated" to avoid manual edits:
# app/Models/User.php
// Auto-generated by Reversify. Do not edit directly.
How can I help you explore Laravel packages today?