- How do I attach metadata to an existing Eloquent model without changing its database table?
- Use the `make:metadata` artisan command to generate a dedicated meta model for your Eloquent class. This creates a separate table for metadata storage while keeping your original model’s schema intact. For example, run `php artisan make:metadata --model=Post` to scaffold metadata support for a `Post` model.
- Does this package support automatic type casting for metadata values (e.g., JSON, dates, booleans)?
- Yes, the package includes automatic type detection and casting for common value types like JSON, dates, booleans, and arrays. When you store a value, it’s intelligently cast to the appropriate format upon retrieval, reducing manual handling in your code.
- What Laravel versions are officially supported by this package?
- The package requires Laravel 8.x, 9.x, or 10.x and PHP 8.0+. Check the `composer.json` of the package for exact version constraints. If you’re using a version outside this range, you may need to adjust dependencies or use composer overrides.
- Can I store metadata in a JSON column instead of a separate table?
- The package defaults to a separate table for metadata storage, which offers better query flexibility and indexing. While JSON columns are possible, they’re not natively supported out of the box and would require custom driver implementation or configuration changes.
- How do I clean up orphaned metadata records when a model is deleted?
- Use the `metadata:clean-orphaned` artisan command to automatically remove metadata records associated with deleted models. This command can be run manually or scheduled via Laravel’s task scheduler to keep your database tidy.
- Will this package work with polymorphic relationships (e.g., a single metadata table for multiple models)?
- The package is designed for model-specific metadata by default, but you can extend its functionality to support polymorphic relationships by customizing the meta model’s table structure or implementing a custom driver. This would require additional configuration beyond the default setup.
- How do I configure caching for metadata to improve performance?
- The package doesn’t include built-in caching, but you can integrate Laravel’s cache system (e.g., Redis) by extending the `MetaModel` class or creating a custom driver. Cache metadata keys using the model’s ID and metadata key, and invalidate the cache when metadata is updated or deleted.
- Are there alternatives to this package for managing model metadata in Laravel?
- Yes, alternatives include `spatie/laravel-activitylog` (for activity tracking), `orchid/platform` (for admin panel metadata), or custom solutions using Laravel’s accessors/mutators. However, this package specializes in flexible key-value metadata storage with artisan tooling for scaffolding and cleanup.
- How do I test metadata functionality in my CI/CD pipeline?
- Test metadata by creating unit tests for your meta models and integration tests for critical workflows like attaching, retrieving, and deleting metadata. Mock Eloquent events if needed, and verify that artisan commands (e.g., `make:metadata`) generate the expected files and migrations.
- What happens if metadata generation fails during a model save (e.g., API timeout for an external driver)?
- The package doesn’t include built-in fallback behavior for external drivers, but you can implement graceful degradation by wrapping metadata operations in try-catch blocks. Log errors and consider retry logic or default values for critical metadata fields.