Skip to content

Releases: brainstudnl/json-api-resource

v5.0.0

02 Mar 08:59
15d3771

Choose a tag to compare

🚀 Major Version Upgrade
This PR upgrades the package to Laravel 12 and PHP 8.5, and removes the deprecated registerData functionality.

⚠️ BREAKING CHANGES

  1. Minimum Requirements
    PHP: Now requires 8.5+ (was 8.2+)
    Laravel: Now requires 12+ (was 8-11)
  2. Removed Deprecated Functionality
    ❌ Removed register() method
    ❌ Removed registerData property
    ✅ Must use method-based resource definitions
  3. 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

06 Feb 15:44
c4c2bbe

Choose a tag to compare

Changed

  • Keep v4 alive by adding a v4 branch.
  • Add matrix strategy to test job #36.

Full Changelog: v4.3.2...v4.3.3

v4.3.1

05 Jan 14:52
ecdd43c

Choose a tag to compare

What's Changed

  • Improve the docblock for toId in JsonApiResource by @brdv in #32
  • Handle null resource more gracefully in toId by @brdv in #34

Full Changelog: v4.3.0...v4.3.1

v4.3.0

28 Apr 08:59
75c6331

Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v4.2.0...v4.3.0

v4.2.0

16 Oct 07:22
0d3ee6e

Choose a tag to compare

What's Changed

  • Add more robust id resolving by @brdv in #31
  • Update readme documentation to new methods by @brdv in #29

Full Changelog: v4.1.1...v4.2.0

v4.1.1

27 Sep 12:20
f9af786

Choose a tag to compare

What's Changed

  • Apply filter to outcome of toRelationships method by @brdv in #30

Full Changelog: v4.1.0...v4.1.1

v4.1.0

27 Sep 08:43
b2f4e38

Choose a tag to compare

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

v4.0.6

08 Jul 07:53
cc2abb2

Choose a tag to compare

What's Changed

  • Allow empty metadata in addMeta method by @brdv in #21
  • Make ContentTooLarge exception PSR-4 compliant by @brdv in #19

Full Changelog: v4.0.5...v4.0.6

v4.0.5

18 Jun 07:28
2c8bdfd

Choose a tag to compare

What's Changed

  • Fix faulty isEmpty() call to empty() by @brdv in #23

Full Changelog: v4.0.4...v4.0.5

v4.0.4

17 Jun 13:56
928dfa4

Choose a tag to compare

What's Changed

  • Map symfony 405 exception to ApiException by @brdv in #22

Full Changelog: v4.0.3...v4.0.4