- How do I dynamically add a column to an Eloquent model without writing a migration?
- Use the `manage()` method on your model (e.g., `User::manage()->addColumn('status', 'string', ['nullable' => true])`). This executes the schema change at runtime. For safety, wrap it in a transaction and validate data compatibility before applying.
- Will this package work with Laravel 9.x or only Laravel 10+?
- The package is officially tested for Laravel 10+, but basic functionality may work on Laravel 9.x. For newer Eloquent features (e.g., `useAsMorphMap`), you’ll need to polyfill or test thoroughly. Check the package’s changelog for Laravel 9.x compatibility notes.
- Can I use this for polymorphic relationships that change at runtime?
- Yes, the package supports dynamic polymorphic relationships. Define them via `addRelationship('morphTo', 'target')` and specify the morph map. However, test query performance—polymorphic joins can degrade with large datasets.
- How does this handle rollbacks if a schema change fails mid-deployment?
- The package doesn’t include built-in rollback logic, but you can manually revert changes using raw SQL or a custom `ManageEloquent::revert()` method. Always wrap operations in transactions and log changes for auditing.
- Does this conflict with spatie/laravel-activitylog or other Eloquent hooks?
- Potential conflicts may arise if packages rely on Eloquent’s `booted()` events or model registration hooks. Test thoroughly, especially for packages that modify model behavior during bootstrapping. Disable caching (e.g., `model:bootstrap`) if issues persist.
- What’s the performance impact of adding a column to a table with millions of rows?
- Runtime schema changes can introduce latency, especially for large tables. For example, adding a column to a 1M-row table may take seconds. Optimize by batching changes, using transactions, and avoiding indexes on dynamic columns unless necessary.
- Can I use this to modify existing columns (e.g., change a string to an enum)?
- Yes, but with caution. Use `alterColumn('column', 'enum', ['values' => ['active', 'inactive']])`. Always back up data first, as altering column types may truncate or corrupt values if not handled properly.
- How do I test dynamic schema changes in my CI pipeline?
- Use Laravel’s `Schema::getColumnListing()` to verify changes in unit tests. For integration tests, reset the database between runs or use tools like `laravel-database-snapshots` to isolate schema mutations. Mock the `ManageEloquent` trait for unit testing.
- Is there a way to restrict dynamic columns to prevent mass assignment vulnerabilities?
- Yes, explicitly define `$fillable` or `$guarded` in your model to control which dynamic columns can be mass-assigned. The package doesn’t auto-fill dynamic columns, but always validate input to avoid unintended data exposure.
- What’s a good alternative if I need runtime schema changes but want zero performance overhead?
- Consider pre-defining all possible columns in migrations and use soft-deletes or nullable columns for optional fields. For true runtime flexibility, evaluate packages like `laravel-model-factory` or `spatie/laravel-model-states`, though they offer less schema control than `manage-eloquent`.