@felixinx which version of Laravel you're using? It seems to be working in Laravel 6.
Jan 7, 2020
12
Level 1
Array casting with index in API resource
Hi,
I am current casting my user_approval field to a JSON array using the following code:
protected $casts = [
'user_approval' => 'array',
];
In artisan, my field is displayed with index and value:
>>> $a->user_approval
=> [
1 => true,
2 => false,
5 => false,
]
But when I use my API resource, only the value show up:
{
"data": [
{
"approval": [
true,
false,
false
],
...
}
}
Here is the code for my resource:
class AnswerResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'approval' => $this->user_approval,
'categoryId' => $this->category_id,
'content' => $this->content,
'id' => $this->id,
'roundId' => $this->round_id,
'user' => UserResource::make($this->user)->resolve()
];
}
}
Is there a way to show index from a array casting inside a API resource?
Thank you
Please or to participate in this conversation.