Releases: brainstudnl/json-api-resource
v5.0.0
🚀 Major Version Upgrade
This PR upgrades the package to Laravel 12 and PHP 8.5, and removes the deprecated registerData functionality.
- Minimum Requirements
PHP: Now requires 8.5+ (was 8.2+)
Laravel: Now requires 12+ (was 8-11) - Removed Deprecated Functionality
❌ Removed register() method
❌ Removed registerData property
✅ Must use method-based resource definitions - Method Visibility Change
toAttributes() is now public (was protected) for Laravel 12 compatibility
📝 Changes Made
Dependencies Updated
PHP: ^8.2 → ^8.5
Laravel Framework: ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 → ^12.0
orchestra/testbench: ^7.22.0 → ^10.0
phpunit/phpunit: ^9.6.3 → ^11.0
Laravel 12 Compatibility Fixes
Added resolveResourceData() override to prevent circular dependency between toArray() and toAttributes()
Changed toAttributes() visibility from protected to public to match Laravel 12's base JsonResource class
Added comprehensive documentation explaining why we override toAttributes() and how it differs from Laravel's implementation
Removed Deprecated Code
Removed private array $registerData property
Removed protected function register(): array method
Removed all fallbacks to registerData in getType(), toAttributes(), toRelationships(), toMeta(), toLinks()
Updated documentation to reflect method-based approach only
Test Resources Converted
All test resources converted from register() to method-based approach:
✅ AccountResource.php
✅ PostResource.php
✅ CommentResource.php
🧪 Testing
All tests passing:
✅ 49 tests
✅ 85 assertions
✅ Full compatibility with Laravel 12 and PHP 8.5
📖 Migration Guide for Users
Step 1: Update Requirements
composer require brainstud/json-api-resource:^5.0
Step 2: Change Method Visibility
Change all protected function toAttributes() to public function toAttributes():
// Before
protected function toAttributes(Request $request): array
// After
public function toAttributes(Request $request): array
Step 3: Convert register() to Method-Based Definitions
Before (deprecated):
protected function register(): array
{
return [
'id' => $this->resource->id,
'type' => 'users',
'attributes' => [
'name' => $this->resource->name,
'email' => $this->resource->email,
],
'relationships' => [
'posts' => ['posts', PostResourceCollection::class],
],
'meta' => [
'verified' => $this->resource->email_verified_at !== null,
],
'links' => [
'self' => route('users.show', $this->resource),
],
];
}
After (required):
protected string $type = 'users';
protected function toId(): string|int|null
{
return $this->resource->id;
}
public function toAttributes(Request $request): array
{
return [
'name' => $this->resource->name,
'email' => $this->resource->email,
];
}
protected function toRelationships(Request $request): array
{
return [
'posts' => ['posts', PostResourceCollection::class],
];
}
protected function toMeta(Request $request): array
{
return [
'verified' => $this->resource->email_verified_at !== null,
];
}
protected function toLinks(Request $request): array
{
return [
'self' => route('users.show', $this->resource),
];
}
🔍 Technical Details
Why Override toAttributes()?
Laravel 12's toAttributes() returns the full resource (same as toArray()), but JSON:API needs it to return only the attributes portion:
Laravel's behavior:
public function toAttributes(Request $request) {
return $this->toArray($request); // Full resource
}
JSON:API behavior:
public function toAttributes(Request $request): array {
return []; // Only attributes portion
}
This override is intentional and necessary to:
Provide JSON:API-specific semantics
Prevent circular dependency in the resource resolution chain
Maintain separation between attributes, relationships, meta, and links
📌 Notes
Users on older PHP/Laravel versions should stay on v4.x
This is a major version bump due to breaking changes
All existing functionality preserved, just modernized for Laravel 12
Full Changelog: v4.3.1...v5.0.0
v4.3.3
v4.3.1
v4.3.0
v4.2.0
v4.1.1
v4.1.0
Release v4.1.0
This release introduces a new way to define resources. It adds four methods to the JsonApiResource that each cover a specific part of a resource. The big difference is that you'll have direct access to the Request in theses methods, as the methods are called only in toArray. This will allow for better/easier access to the specific Request your resource is built for.
It also deprecates the use of the register method, as going forward, using the methods to define your resource is the preferred way. For now, we keep the options to use register, but know that it might be removed in a future release.
What's Changed
- Update laravel pint by @brdv in #25
- Rename CollectionResource to ResourceCollection to better follow Laravel by @brdv in #26
- Add option to define resources by methods by @brdv in #24
- Type getId method as string or int by @brdv in #28
- Allow for empty attributes in response by @brdv in #27
Full Changelog: v4.0.6...v4.1.0