Can I use transformers to transform data coming from API rather than from database?
$this->response->collection
is meant to get a collection of objects, not the array. Then all of this objects is going to transformer that is transform OnboardingEntity objects as you want. So first you should transform your input array into collection of objects. The example how i did it above (you should change it to your own input array)
$data = json_decode('[
[
{
"companyID": "U72400MHTC293037",
"companyName": "pay pvt LIMITED"
},
{
"companyID": "U74900HR2016PT853",
"companyName": "dddd PRIVATE LIMITED"
}
]
]');
$data = collect( array_map( function($ob){
return (new OnboardingEntity($ob));
}, $data[0]));
And then pass this collection of OnboardingEntity objects to $this->response->collection
method, like here
$this->response->collection($data,new TestTransformer());
You may want to send common data structure to Fractal as sources of data are different. Array is best possible type for you.
Consider this when you are fetching data from Eloquent(DB):
$result = $yourModel->get(); // This will return you with a collection object.
Before passing this object to fractal convert it to array.
$this->response->collection($result->toArray(),new OnboardingTransformer());
In case of first
or single model object. check for null before calling toArray()
.
$result = $yourModel->first();
if($result){
$result = $result->toArray();
}
// Fractal itself can handle null
Now for second scenario where data is coming from external source like API or file.
$result = $this->my_service->getDetailsByName($company_name);
// Try converting your response object to array from within
You can do this with json_decode(<Body of response>, true)
. And then pass this array to Fractal.
Why Array?
Because source of data could be anything from Database to File, from Cache to APIs. Format could be JSON or XML. Converting all these to array comes built in PHP.