- How do I add dynamic properties to a Laravel Eloquent model using this package?
- First, add the `HasCustomProperties` trait to your model and include a `custom_properties` JSON column in your database table. Then use methods like `setCustomProperty()`, `getCustomProperty()`, or `forgetCustomProperty()` to manage dynamic attributes. Always call `save()` to persist changes.
- Which Laravel versions does this package support?
- The package officially supports Laravel 7 and 8, with partial compatibility for Laravel 6. For Laravel 9/10, test JSON column handling and Facade methods manually, as the package hasn’t been updated since 2020. Laravel 6 may require polyfills for JSON serialization.
- Does this package work with MySQL 5.6 or older databases?
- No, this package requires JSON column support, which is only available in MySQL 5.7+. For older versions, consider a fallback like a `custom_properties_text` column or upgrade your database. PostgreSQL and SQLite 3.9+ are fully supported.
- How do I handle nested JSON structures or circular references with custom properties?
- The package uses PHP’s native `json_encode()` and `json_decode()`, which may fail on circular references or deeply nested data. Validate input before setting properties and handle exceptions gracefully. For complex cases, pre-process data or use a library like `spatie/array-to-xml` for serialization.
- Can I use this package for production APIs with high write concurrency?
- While the package is lightweight, concurrent writes to the `custom_properties` JSON column could lead to race conditions. Test under load and consider adding application-level locks or database transactions for critical operations. JSON serialization/deserialization adds minimal overhead.
- What happens if the `custom_properties` column is corrupted or malformed?
- If the JSON column contains invalid data, Eloquent will throw a `JsonException`. Handle this in your model’s `retrieved` event or use a `try-catch` block when accessing properties. As a fallback, you could reset the column to `null` or a default value.
- How do I perform partial updates (e.g., PATCH requests) for nested JSON structures?
- The package doesn’t natively support partial updates, but you can manually merge arrays using `array_merge()` or libraries like `spatie/array-to-xml`. For example, update nested properties with `$model->setCustomProperty('nested.key', $value)` and save the model.
- Are there alternatives to this package for dynamic model properties?
- Yes. For Laravel, consider `spatie/laravel-activitylog` (for structured logs), `stancl/tenancy` (multi-tenancy with dynamic attributes), or `laravel-model-attributes` for more flexible attribute handling. For NoSQL-like flexibility, use `spatie/laravel-json-attributes` or store data in a separate table.
- How do I test this package in my Laravel application?
- Test CRUD operations with dynamic properties, including edge cases like empty JSON, large payloads, and concurrent writes. Use Laravel’s `ModelObserver` or `retrieved` events to validate data integrity. Mock the JSON column in unit tests with `DB::shouldReceive()` or factory seeds.
- Can I use this package with Laravel Scout for searchable custom properties?
- No, JSON columns in `custom_properties` cannot be indexed by Scout. For searchable dynamic attributes, consider storing them in a dedicated table or using a full-text search engine like Algolia. Alternatively, use `json_extract()` in MySQL 8+ for limited querying.