Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Reversify Laravel Package

abevanation/reversify

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require abevanation/reversify
    php artisan vendor:publish --tag=reversify-config
    

    Review the published config at config/reversify.php.

  2. First Use Case: Reverse-engineer an existing database table into Laravel components:

    php artisan reversify:generate --table=users
    

    This generates:

    • A migration file in database/migrations/.
    • An Eloquent model in app/Models/.
    • A CRUD controller in app/Http/Controllers/.
  3. Where to Look First:

    • Configuration: config/reversify.php (customize naming conventions, paths, and defaults).
    • Artisan Commands:
      • reversify:generate (core command).
      • reversify:model (generate only models).
      • reversify:controller (generate only controllers).
    • Example Output: Check tests/ in the package repo for sample outputs.

Implementation Patterns

Usage Patterns

  1. Batch Generation: Generate components for multiple tables at once:

    php artisan reversify:generate --tables=users,posts,comments
    
  2. Customizing Output: Override defaults via config or command options:

    php artisan reversify:generate --table=users --model-path=App/Entities --controller-path=App/Http/Resources
    
  3. Integrating with Existing Code:

    • Models: Extend generated models with custom logic in app/Models/User.php:
      class User extends \GeneratedUser {
          public function customScope() { ... }
      }
      
    • Controllers: Override methods in generated controllers:
      class UserController extends \GeneratedUserController {
          public function customMethod() { ... }
      }
      
  4. Relationship Handling: Reversify auto-detects foreign keys and generates relationships. Manually adjust in the model:

    public function posts() {
        return $this->hasMany(Post::class);
    }
    
  5. 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"
    

Workflows

  1. Legacy System Migration:

    • Reverse-engineer the entire schema:
      php artisan reversify:generate --all-tables
      
    • Refactor generated code incrementally (e.g., add validation, policies).
  2. Rapid Prototyping:

    • Generate a model + controller for a new feature:
      php artisan reversify:generate --table=products --controller=yes
      
    • Extend the controller with business logic.
  3. API Development:

    • Generate models with API-specific attributes (e.g., hidden, appends):
      php artisan reversify:generate --table=users --with-attributes="hidden:password,remember_token"
      

Integration Tips

  • 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
    

Gotchas and Tips

Pitfalls

  1. Foreign Key Ambiguity:

    • Reversify may misinterpret complex foreign key relationships (e.g., polymorphic or many-to-many).
    • Fix: Manually adjust relationships in the generated model after creation.
  2. Reserved Keywords:

    • Tables/columns with reserved keywords (e.g., order, group) may cause syntax errors.
    • Fix: Use --prefix flag or rename in the migration:
      Schema::rename('order', 'orders');
      
  3. Circular Dependencies:

    • Generating models for interdependent tables (e.g., users and roles) may require manual ordering.
    • Fix: Generate tables in dependency order or use --skip-relationships.
  4. Configuration Overrides:

    • Command-line flags override config, but config overrides defaults. Test thoroughly:
      php artisan reversify:generate --table=users --model-path=CustomPath
      
  5. Soft Deletes:

    • Generated models with SoftDeletes may conflict with existing deleted_at columns.
    • Fix: Exclude the trait or adjust the config:
      'traits' => [
          'SoftDeletes' => false,
      ],
      

Debugging

  1. Verbose Output: Enable debug mode in config:

    'debug' => true,
    

    Or use the --verbose flag:

    php artisan reversify:generate --table=users --verbose
    
  2. Dry Runs: Preview changes without generating files:

    php artisan reversify:generate --table=users --dry-run
    
  3. Logging: Check storage/logs/laravel.log for errors during generation.

Tips

  1. Partial Generation: Generate only migrations or models separately:

    php artisan reversify:model --table=users
    php artisan reversify:migration --table=users
    
  2. Custom Templates: Override Blade templates for models/controllers in resources/views/vendor/reversify/:

    • Copy from vendor/abevanation/reversify/resources/views/ to your project.
    • Modify as needed (e.g., add custom methods to controllers).
  3. Excluding Tables: Skip system tables in config:

    'excluded_tables' => ['migrations', 'failed_jobs'],
    
  4. 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
  1. 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'));
    }
    
  2. 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.
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor