Laravel从集合中删除[data]

时间:2015-10-02 04:05:58

标签: php laravel laravel-5

我尝试使用eloquent和fractal进行查询

 $lists = Category::all();

 $result = Fractal::collection($lists, new CategoryTransformer())->getArray();

并将其退回

 return response()->json((['code' => "200", 'results' => $result]));

json结果如下:

{"code":"200","results":{"data":[{"id":"1","name":"Cafe","logo":null,"cover":""},{"id":"2","name":"SPA","logo":null,"cover":""},{"id":"3","name":"Hotel","logo":null,"cover":""}]}}

如何删除"数据"结果?所以我可以在没有"数据"。

的情况下获得阵列

我试过了:

 $result = Fractal::collection($lists, new CategoryTransformer(), 'results')->getArray();

 return (['code' => "200", $result]);

它让我回复:

   {"code":"200","0":{"results":[{"id":"1","name":"Cafe","logo":"","cover":""},{"id":"2","name":"SPA","logo":"","cover":""},{"id":"3","name":"Hotel","logo":"","cover":""}]}}

领先' 0'结果之前。我怎么能删除它?

由于

3 个答案:

答案 0 :(得分:1)

试试这个:

return (['code' => "200", "results" => $result['results']);

我认为数组方法无法处理给定的数组。

另一种解决方案是添加结果:

$result['code'] = 200;
return $result;

答案 1 :(得分:0)

数据只是关键,我认为它不会出现任何问题。如果仍需要删除它,请更新getArray()函数。

答案 2 :(得分:0)

将这些收集宏放在AppServiceProvider::boot()方法中:

/**
 * Remove the unnecessary nested 'data' keys
 *
 * @param string $case For consistency, define the type of keys that should be returned
 */
Collection::macro('fractal', function ($case = 'snake_case') {
    //Handle this as a nested function to block access to the $depth flag.
    //It's purpose is to indicate how deep the recursion is, and,
    //more importantly, when it's handling the top-level instance
    $recursion = function ($case = 'snake_case', array $items = [], $depth = 0) use (&$recursion) {
        //If the array has only one element in it, and it's keyed off 'data', remove the wrapper.
        //However, if it has a sibling element, such as 'meta', leave it alone
        if (array_key_exists('data', $items) && count($items) == 1) {
            $items = $items['data'];
        }

        $items = (new static($items))->mapWithKeys_v2(function ($item, $key) use (
            $case,
            $recursion,
            $depth
        ) {
            $key = $case ? $case($key) : $key;

            //If the nested item is itself an array, recursively perform the same transformation
            return is_array($item) ?
                [$key => $recursion($case, $item, ++$depth)] : [$key => $item];
        })->toArray();

        //Maintain the top-level 'data' wrapper.
        //This can easily be removed later in the controller if that's not needed either
        $items = (!$depth && !array_key_exists('data', $items)) ?
            ['data' => $items] : $items;

        return $items;
    };

    //Return the results in the form of an instance of Collection
    return new static($recursion($case, $this->items));
});

/**
 * Maintain non-sequential numeric keys when performing
 * \Illuminate\Support\Collection::mapWithKeys() functionality
 *
 * Source: https://github.com/laravel/framework/issues/15409#issuecomment-247083776
 */
collect()->macro('mapWithKeys_v2', function ($callback) {
    $result = [];
    foreach ($this->items as $key => $value) {
        $assoc = $callback($value, $key);
        foreach ($assoc as $mapKey => $mapValue) {
            $result[$mapKey] = $mapValue;
        }
    }
    return new static($result);
});

然后通过它运行你的Fractal结果: $results = collect($fractalResults)->fractal('camel_case')->get('data', []);

相关问题