League \ Fractal Response内容必须是实现__toString()的字符串或对象,给出“object”
问题描述:
I have two implementations of Transformer: one I made with arrays and the other I made with using of League\Fractal
package. So first implementation:
$averages = [];
foreach ($bigData as $row)
{
$avgTemp = new StatisticsDataset($row->air_temp);
$avgBar = new StatisticsDataset($row->bar_press);
$avgWind = new StatisticsDataset($row->wind_speed);
$averages[] = [
'Data Recorded' => $row->data_recorded,
'Mean Temperature' => $avgTemp->getMean(),
'Median Temperature' => $avgTemp->getMedian(),
'Mean Pressure' => $avgBar->getMean(),
'Median Pressure' => $avgBar->getMedian(),
'Mean Speed' => $avgWind->getMean(),
'Median Speed' => $avgWind->getMedian()
];
}
return $averages;
Works great and there're no errors. And I have second one, that has an issue:
$resource = new Fractal\Resource\Collection($envData, function(EnviromentalData $env) {
return [
'Data Recorded' => $env->data_recorded,
'Mean Temperature' => ((new StatisticsDataset($env->air_temp))->getMean()),
'Median Temperature' => ((new StatisticsDataset($env->air_temp))->getMedian()),
'Mean Pressure' => ((new StatisticsDataset($env->bar_press))->getMean()),
'Median Pressure' => ((new StatisticsDataset($env->bar_press))->getMedian()),
'Mean Speed' => ((new StatisticsDataset($env->wind_speed))->getMean()),
'Median Speed' => ((new StatisticsDataset($env->wind_speed))->getMedian())
];
});
Error: UnexpectedValueException in Response.php line 403:
The Response content must be a string or object implementing __toString(), "object" given.
So the question is what it could be?
答
Forgot to add at the end of the file
return $fractal->createData($resource)->toJson();