php - how to Data manipulation in laravel resource
one text
i want to manipulate my data in resource collection this is my Resource Collection :
requestState is my Model Relationship
class RequestResource extends JsonResource
{
public function toArray($request)
{
return [
'requestId'=>$this->id,
'user'=>$this->user->profile->name." ".$this->user->profile->family,
'submitDate'=>$this->created_at,
'requestState'=>$this->requestState()->latest('decided_at')->first(),
'field'=>'kardani computer',
];
}
}
this is json that return from the resource
{
"data": {
"requestId": 63,
"user": "???�?�?�?� ?�?�?�?�?�??",
"submitDate": "2021-05-17T13:00:48.000000Z",
"requestState": {
"id": 27,
"request_id": 63,
"confirm": null,
"description": null,
"decided_at": "2021-06-23 17:30:48",
"created_at": "2021-05-17T13:00:48.000000Z",
"updated_at": "2021-05-17T13:00:48.000000Z",
"department_id": 3
},
"field": "kardani computer"
}
}
i want to manipulate "requestState" relation model Data and show whatever i want or change data and return in RequestResource Do I need to define a resource for RequestState ? And call it in RequestResource?
'requestState'=>$this->requestState()->latest('decided_at')->first(),
Source