dingo / dingo/api

Help: Casts not happening before API response

Open
#1,216 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
9.4k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Regarding the use of https://laravel.com/docs/master/eloquent-mutators#attribute-casting with example:

```
'array',
];
}
```

When I use the native PHP accessor

```
$user = \App\User::find(1);
$user->options['thing'] = 'stuff';
$user->save();
```

The JSON is automagically converted to an array, the key is updated, and then converted back to JSON when saved. This works as expected.
With Dingo in the mix I am responding to an API request:

```
// SomeController.php
public function listUsers()
{
$users = User::all();
// At this point each $user->options appears to still be a STRING of JSON data, it is not de-serialized until I try to access it as a $user->options property.
$response = [
'success' => true,
'message' => '',
'users' => $users,
];
return response()->json($response);
// My hope is that the Dingo response would notice that there are casts and somehow de-serialize the data before building a JSON response for the user. Is this an incorrect use?
}
```

Dingo is not responding with the user array like in the accessor, but it is instead responding with the string of JSON. I can manually decode this JSON string within the Dingo JSON response manually, but that is rather odd to have to do.

Is there a better way to respond to the request that will properly de-serialize the JSON $user->options property before sending the response back to the user? Or is this not currently supported?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.