I have been using laravel to build my APIs. I use transformers to tranform data from model object.
Now instead of database, I have a response coming from an API as the data source and I want to transform that data back to a user, but I am unable to do so.
My Controller
public function rocByName(Request $request)
{
try {
$this->roc_by_name_validator->with( $request->all() )->passesOrFail();
$company_name = $request->input('company_name');
$result = $this->my_service->getDetailsByName($company_name); //$result has the response object from the API which I want to transform and give it as a response.
return $this->response->collection($result,new OnboardingTransformer()); //Tried using tranformer like this
}
catch (ValidatorException $e) {
dd($e);
}
}
My Transformer
<?php
namespace Modules\Onboarding\Transformers;
use League\Fractal\TransformerAbstract;
use App\Entities\OnboardingEntity; //I dont have an entity since the response is coming from an API!! What will give here?
/**
* Class OnboardingTransformerTransformer
* @package namespace App\Transformers;
*/
class OnboardingTransformer extends TransformerAbstract
{
/**
* Transform the \OnboardingTransformer entity
* @param \OnboardingTransformer $model
*
* @return array
*/
public function transform(OnboardingEntity $data_source)
{
return [
'company_name' => $data_source->company_name,
];
}
}
Here the OnboardingEntity refers to data coming from database ideally. Here I am not fetching data from database, instead my data is from an API source. How do I go about it. I am little consfused here. Can someone give a solution?
$result has the following response
[
[
{
"companyID": "U72400MHTC293037",
"companyName": "pay pvt LIMITED"
},
{
"companyID": "U74900HR2016PT853",
"companyName": "dddd PRIVATE LIMITED"
}
]
]
Data transformers are used to translate the data for a field into a format that can be displayed in a form (and back on submit). They're already used internally for many field types. For example, the DateType field can be rendered as a yyyy-MM-dd -formatted input text box.
Basically, transformers give you the flexibility to create a format for JSON response that you need. By using transformers we can also do type casting, pagination results, and also nest relationships. If you used Dingo API then the Fractal Transformers module already installed.
$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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With