- Can sylius/resource work with Laravel’s Eloquent ORM, or is it only for Doctrine?
- sylius/resource is ORM-agnostic but requires a custom `ResourceRepository` for Eloquent. You’ll need to extend `EloquentResourceRepository` and implement your repository interface. Doctrine users get built-in support with `DoctrineResourceRepository`, reducing setup time.
- How does sylius/resource integrate with Laravel’s built-in validation (e.g., FormRequest)?
- The package doesn’t replace Laravel’s validation but works alongside it. You can use Laravel’s `FormRequest` classes to validate incoming data, then map the validated data to your resource entities. The resource layer handles persistence, while validation remains decoupled.
- What’s the performance impact of using sylius/resource compared to raw Eloquent queries?
- The overhead is minimal for most CRUD operations. The abstraction adds a thin layer between your domain logic and the repository, but queries remain optimized. For complex queries, you can still use Eloquent’s query builder directly within custom repository methods.
- How do I migrate an existing Eloquent model to use sylius/resource without breaking changes?
- Start by creating a `ResourceInterface` for your model, then implement a custom `ResourceRepository` (e.g., `EloquentResourceRepository`). Gradually replace direct `Model` usage in controllers/services with resource methods. Use IDE refactoring tools to simplify the transition.
- Does sylius/resource support soft deletes or other advanced Eloquent features?
- Soft deletes require custom implementation in your `ResourceRepository`. Extend `EloquentResourceRepository` and override methods like `delete()` to use Eloquent’s `forceDelete()` or soft-delete logic. The package doesn’t enforce soft deletes by default to keep it flexible.
- Can I use sylius/resource with Laravel’s API Resources for JSON serialization?
- sylius/resource handles serialization differently but can coexist with Laravel’s `ApiResource`. You can manually transform resources to JSON in controllers or use a hybrid approach where resources are serialized via their own methods while leveraging `ApiResource` for API-specific logic.
- What’s the learning curve for a Laravel team new to DDD or Sylius’s resource pattern?
- Teams unfamiliar with DDD may take 2–4 weeks to adapt, depending on complexity. Focus on understanding `ResourceInterface`, repositories, and events. Start with simple resources (e.g., `User`, `Product`) to build intuition before tackling complex aggregates.
- How do I handle domain events (e.g., ResourceCreated) in Laravel’s event system?
- sylius/resource fires domain events (e.g., `ResourceCreated`, `ResourceUpdated`) that you can dispatch to Laravel’s event system. Listen for these events in your `EventServiceProvider` and trigger side effects like notifications or logging. Example: `event(new ResourceCreated($resource));`
- Are there alternatives to sylius/resource for DDD in Laravel, and how do they compare?
- Alternatives include `spatie/laravel-model-based-api-resources` (simpler, API-focused) or `asgardcms/common` (monolithic, less flexible). sylius/resource stands out for its DDD alignment, event-driven design, and ORM flexibility, making it ideal for complex domain logic.
- How do I test controllers or services that use sylius/resource with PHPUnit?
- Mock the `ResourceRepositoryInterface` in your tests to isolate domain logic. Use PHPUnit’s mock builder to create fake repositories, then inject them into your services/controllers. For integration tests, use in-memory databases or seed test data via repositories.