- How does this package handle bidirectional relations like User ↔ Post in Laravel APIs?
- The package adds a `_mapping_bidirectional_relation` key to serialized JSON, embedding metadata to reconstruct relations during deserialization. For example, a `Post` with an `author` field will include a reverse mapping to the `User`'s `posts` collection, ensuring both sides are restored correctly. This works seamlessly with Eloquent models and JMS Serializer’s event system.
- Can I use this with Laravel 9+ without Symfony components?
- Yes, but you’ll need to manually configure the subscribers via `SerializerBuilder` (as shown in the README). Laravel’s native DI container won’t auto-register the subscribers without Symfony’s `jms_serializer.event_subscriber` tag. For standalone use, the first installation example in the README covers this setup.
- What Laravel versions are officially supported?
- The package itself isn’t Laravel-specific, but it works with Laravel 5.5+ due to JMS Serializer’s Symfony DI integration. For Laravel 8/9, you may need to manually resolve dependencies or use a compatibility layer like `symfony/dependency-injection` if Symfony isn’t already in your project.
- How do I exclude specific fields from bidirectional mapping?
- Add the `@ExcludeFromMapping` annotation to any field you want to skip during serialization/deserialization. For example, annotate a `Post`'s `deleted_at` field to prevent it from being included in the bidirectional relation graph. This is useful for sensitive or non-relational data.
- Will this work with many-to-many relations (e.g., User ↔ Role) or only one-to-many?
- The package supports one-to-many and many-to-one relations out-of-the-box. For many-to-many (e.g., pivot tables), you’ll need to manually configure the relation mappings using the `@SerializerBidirectionalRelation` annotation on the root model and ensure the pivot data is serializable. Test with your specific schema.
- Is there a performance impact compared to native Laravel serialization?
- Yes, bidirectional mapping adds overhead due to the extra metadata (`_mapping_bidirectional_relation`) and event subscriber processing. For high-throughput APIs, benchmark serialization/deserialization with and without this package. If performance is critical, consider alternatives like `spatie/laravel-arrayable` for simpler cases.
- How do I test bidirectional relations in PHPUnit?
- Serialize an object graph, then deserialize it and assert that both sides of the relation are correctly populated. For example, serialize a `User` with `posts`, then deserialize and verify the `Post`’s `author` points back to the original `User`. Use Laravel’s `JsonTestResponse` or JMS Serializer’s built-in assertions for validation.
- What happens if I deserialize data without the `_mapping_bidirectional_relation` key?
- The package will silently skip relation reconstruction for objects without the mapping key. This prevents errors but means bidirectional relations won’t be restored. Ensure your serialization process always includes the mapping metadata for critical object graphs.
- Are there alternatives if I don’t want to use JMS Serializer?
- For Laravel, consider `spatie/laravel-arrayable` for simple serialization or Symfony’s native `Serializer` component (with custom `Denormalizer` interfaces) for bidirectional support. If you’re already using JMS Serializer, this package is the most direct solution, but migrating to Symfony’s serializer would eliminate dependency risks.
- How do I handle circular references (e.g., User ↔ Post ↔ User) with this package?
- The package doesn’t natively handle circular references, which can cause infinite loops during serialization. Use JMS Serializer’s built-in `@MaxDepth` annotation or Symfony’s `CircularReferenceHandler` to limit depth. For complex cases, manually break cycles in your model logic before serialization.