League \ Fractal Response内容必须是实现__toString()," object"的字符串或对象。特定

时间:2015-09-18 15:50:58

标签: php laravel laravel-5.1

我有两个Transformer实现:一个是我使用数组创建的,另一个是使用League\Fractal包创建的。所以首先实施:

    $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;

效果很好,没有错误。我有第二个问题,有一个问题:

$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())
            ];
        });

错误:UnexpectedValueException in Response.php line 403: The Response content must be a string or object implementing __toString(), "object" given.

所以问题是它可能是什么?

1 个答案:

答案 0 :(得分:0)

忘记在文件末尾添加

return $fractal->createData($resource)->toJson();