- How do I install Enhanced Resources in a Laravel 13.x project?
- Run `composer require sourcetoad/enhanced-resources` in your project directory. The package is officially compatible with Laravel 13.x and PHP 8.3+, so no additional configuration is needed beyond the installation.
- Can I use this package with Laravel 10.x or older versions?
- The package is optimized for Laravel 13.x and PHP 8.3+, but it may work with older versions. However, no long-term support is guaranteed for Laravel <10.x, so test thoroughly if you must use it there.
- How do I define multiple formats for a single API Resource?
- Use the `#[Format]` attribute on each method representing a format. For example, methods `foo()` and `bar()` will create formats named `foo` and `bar`. Call them explicitly with `->format('foo')` or set a default with `#[IsDefault]`.
- What happens if I don’t specify a format and there’s no default?
- The package throws a `NoFormatSelectedException`. To avoid this, always define a default format using `#[IsDefault]` on one of your `#[Format]` methods, or explicitly specify the format when resolving the resource.
- Does this package work with Laravel Octane or real-time APIs?
- Yes, Enhanced Resources integrates seamlessly with Laravel Octane and real-time APIs. Use it to dynamically structure WebSocket payloads or Event Bus messages by leveraging multiple formats for different client needs.
- Will this package slow down my API responses in Laravel 13?
- Laravel 13’s optimizations (OpCache, JIT, and service container improvements) should mitigate overhead. Profile with tools like Laravel Telescope or Blackfire to measure performance, especially for nested relationships.
- Can I use this with Laravel Livewire or Inertia.js?
- Yes, Enhanced Resources works with Livewire and Inertia.js since they both rely on Laravel’s API Resources under the hood. Use it to customize payloads for different frontend states or client-side logic.
- How do I test Enhanced Resources in Laravel 13 with Pest?
- Use Pest 2.0’s live assertions to validate payloads. For example, test format outputs with `assertJson($resource->format('foo'))` or snapshot testing for complex responses. Laravel 13’s new testing helpers simplify this process.
- Are there alternatives to Enhanced Resources for dynamic API payloads?
- Alternatives include custom resource classes with conditional logic, Laravel’s built-in `JsonResource`, or packages like `spatie/array-to-object`. However, Enhanced Resources provides a cleaner, attribute-driven approach for managing multiple formats without manual switching.
- How do I debug issues with nested relationships in Enhanced Resources?
- Use Laravel Telescope 5.0+ to log queries and identify N+1 issues. Enable query logging in `config/telescope.php` and check for inefficient nested eager loading in your format methods.