Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AxelG's avatar
Level 1

Add meta to JSON:API resource relationships

Hi, I am using the native JSON:API implementation added in Laravel 13 to gererate resources from my models.

One of my models have a many-to-many relationship with pivot values. I want them to be included in the relationship on the resource object.
With the help of laraveljsonapi package documentation and AI, I found that I am supposed to put the pivot values in the meta object of my relationship, however I can't find in Laravel documentation how to do it.

Does someone know how to put meta information on a relationship object using Laravel native JSON:API resource feature?

Thanks.

0 likes
5 replies
imrandevbd's avatar

The v13 native JSON:API implementation is solid, but the docs are definitely a bit light on the deeper relationship customizations right now.

You can chain the meta() method directly onto your relationship definition. Since you're dealing with pivot values, you can pass a closure to meta() which receives the related model, allowing you to pull those pivot fields dynamically.

1 like
AxelG's avatar
Level 1

Hi, thanks for your reply!

However, meta() is a function from the laraveljsonapi package, I don't think it exists in native Laravel 13. Moreover, I don't understand how I'm supposed to "chain it on my relationship definition" because the definition is litteraly just a string or callable.

1 like
imrandevbd's avatar

My bad you're right, I crossed the streams with the package syntax there.

In native Laravel 13, you need to use the Relation class within your resource's relationships() method. Instead of just returning a string or a simple callable, you return a relation object, which actually has the meta() method built-in.

Try this structure:

use Illuminate\Http\Resources\JsonApi\Relation;

public function relationships(Request $request): array
{
    return [
        'items' => Relation::toMany('items')
            ->meta(fn ($item) => [
                'pivot_data' => $item->pivot->your_column,
            ]),
    ];
}
1 like
AxelG's avatar
Level 1

For further reference, here is how I solved my problem:

I gave up on my idea, Laravel 13 just doesn't implement a feature at the moment to add meta on a relationship.

Instead, I turned my pivot table into a model and splitted the many-to-many relationship in two one-to-many relationships.

This is more work, but it is more standard than pivot values in meta, and Laravel just has no support for this right now.

Please or to participate in this conversation.