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
3 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.

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.

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,
            ]),
    ];
}

Please or to participate in this conversation.