Skip to content

Conversation

@lukaskleinschmidt
Copy link
Contributor

@lukaskleinschmidt lukaskleinschmidt commented Jul 8, 2021

Currently when doing something like shown in the example below you will end up with an array containing only the values defined with the "dot" notation. This is due to the limitations of Arr::set() only working on arrays and the fact that the API Resource returns a stdClass rather than an array. Changing Arr::set() to data_set will allow you to use the "dot" notation on Objects as well.

In the example you could probably handle the relation in the OrderResource itself but you would miss out on the partial reload or lazy data feature in such cases.

<?php

use App\Http\Resources\AddressResource;
use App\Http\Resources\OrderResource;

return inertia('Orders/Show', [

    'order' => OrderResource::make($order),

    'order.address' => fn () => AddressResource::make($order->address),

]);

// Resulting data using Arr::set()
[
    'order' => [
        'address' => [
            // …
        ]
    ],
]

// Resulting data using data_set()
[
    'order' => [
        'id' => 1,
        'number' => 350647,
        // …
        'address' => [
            // …
        ]
    ],
]

@reinink reinink merged commit bfb6ddf into inertiajs:master Jul 12, 2021
@reinink
Copy link
Member

reinink commented Jul 12, 2021

Looks good! Let's hope data_set never gets deprecated. 😅

@reinink
Copy link
Member

reinink commented Jul 12, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants