Replies: 3 comments 6 replies
-
This is the default since it follows JSON:API spec and Laravel Resource defaults, why not disable wrapping in data and then wrap the whole thing within your controller in an array with data? |
Beta Was this translation helpful? Give feedback.
-
@rubenvanassche As zigi05 already mentioned, in Laravel nested resources aren't wrapped by default. Is there a possibility to disable this behavior? |
Beta Was this translation helpful? Give feedback.
-
Not a full solution, but I did discover a workaround for now (for v4): This method is more of a "soft global" wrap. Disable global wrapping in <?php
namespace App\Data;
use Spatie\LaravelData\Data;
class DataWithoutNestedWrapping extends Data
{
public function defaultWrap(): string
{
return 'data';
}
} And use it instead of Data, like so: class UserData extends DataWithoutNestedWrapping
{
public function __construct(
// ...
#[DataCollectionOf(SometimesNestedData::class)]
public ?DataCollection $sometimesNested,
) {}
}
class SometimesNestedData extends DataWithoutNestedWrapping
{
public function __construct(
// ...
) {}
} It does not appear that any of the nested collections get wrapped. You can also do this with a trait without using this intermediary class but then you have to add an additional line to every class which I felt was a bit too tedious. |
Beta Was this translation helpful? Give feedback.
-
How do I disable wrapping on a nested collection in the following situation, when I have global wrapping enabled?
So, from a controller I get json like this, but I don't want to have "data" after "posts":
Beta Was this translation helpful? Give feedback.
All reactions