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

AxelG's avatar

AxelG wrote a reply+100 XP

1w ago

Add meta to JSON:API resource relationships

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.

AxelG's avatar

AxelG wrote a reply+100 XP

1w ago

Add meta to JSON:API resource relationships

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.

AxelG's avatar

AxelG started a new conversation+100 XP

1w ago

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.

AxelG's avatar

AxelG wrote a reply+100 XP

1mo ago

Polymorphic resources in new JSON:API resources

Ok, I figured it out. Your first answer do work, I was having troubles because of the way I made the call.

For some reason, the additional parameter I was passing to the route function was included in the body of the request instead of the query, unlike the documentation says.
It was working fine with a get request, but not a post. I appended manually the parameters at the end of the url and it works like a charm.

Thanks for your help!

AxelG's avatar

AxelG wrote a reply+100 XP

1mo ago

Polymorphic resources in new JSON:API resources

I'm a bit confused, the doc doesn't mention that you have to manually set the includes, and the withIncludes() method you mentioned doesn't appear anywhere... I also didn't find anything about this function on google. Can you point me to the source?

AxelG's avatar

AxelG wrote a reply+100 XP

1mo ago

Polymorphic resources in new JSON:API resources

Thanks for your answer!

I tried your solution, however when I call POST inventories/1/items?include=itemable.reference (which was my actual test case) and dump the response content, I only see the itemable relationship.
From some other tests I made, I think this is normal because nested relationships are only shown in the included section of the response. However, what is not normal is that the response is missing the included part...

I am calling my route in a test, here is the relevant part:

$response = $this->postJson(
    route('inventories.items.store', [
        'inventory' => $inventory->id,
        'include' => 'itemable.reference',
    ]),
    $data,
);
dump($response->json());

The dump gives me

array:1 [
  0 => array:4 [
    "id" => "21"
    "type" => "items"
    "attributes" => array:3 [
      "id" => 21
      "itemable_id" => 21
      "itemable_type" => "consumable"
    ]
    "relationships" => array:1 [
      "itemable" => array:1 [
        "data" => array:2 [
          "id" => "21"
          "type" => "consumables"
        ]
      ]
    ]
  ]
]

And here is my controller function:

public function store(Inventory $inventory, StoreItemRequest $request, ItemService $itemService)
{
    $items = $itemService->hydrateItems($request->items);

    $items = $itemService->addOrStackItems($inventory, $items);
    return response($items->toResourceCollection(), 201);
}

The $items variable being cast as a ResourceCollection is a Collection of Item models.

Do you see anything that I'm doing wrong?

AxelG's avatar

AxelG started a new conversation+100 XP

1mo ago

Polymorphic resources in new JSON:API resources

I am trying to use the new JSON:API resource feature added in Laravel 13, but I don't see anything in the doc related to polymorphic relations.
The doc about this feature is in the Eloquent section, and I understand that it is built on top on Eloquent, so why is there no mention of polymorphic relations? And more importantly, how can I make it work?

Suppose I have an Inventory model that has an items relationship to an Item class which is polymorphic through the itemable relationship. The itemable can either return a Consumable or Serialized object. How should I define the toRelationships on the ItemResource class to return either a ConsumableResource or a SerializedResource?

I found this solution for the moment:

public function toRelationships(Request $request)
{
    $itemableResourceClass = "App\\Http\\Resources\\" . class_basename(Relation::getMorphedModel($this->itemable_type)) . "Resource";
    return [
        'itemable' => $itemableResourceClass,
    ];
}

But it doesn't work with nested includes like GET /inventories?include=items.itemable.reference.
I get the correct itemable resource, but the reference relationship is not included in the resource.

Also it is not very clean.