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