2015-10-02 111 views
0

刪除[數據]我試過用雄辯和分形Laravel從收集

$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'。我如何刪除它?

感謝

+0

''result'沒有' - > getArray()'的結果是什麼? – aldrin27

+0

嗨,感謝您的回答。 結果爲空{}。 – ssuhat

+0

你可以使用foreach操作你的數組 – aldrin27

回答

1

試試這個:

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

我認爲陣列方法無法處理給定的陣列。

另一個解決方法是添加你的結果:

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

我已經使用第二種解決方案。實際上它與我的有點不同。但它也很棒!我正在使用數組合並。謝謝! – ssuhat

0

數據僅僅是關鍵,我認爲它不會做任何的問題。如果您仍然需要刪除它,請更新getArray()函數。

+0

以及我的應用程序崩潰,如果有任何數據。這就是爲什麼我試圖刪除它。 獲得數組函數只返回數組沒有什麼奇特的有 – ssuhat

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); 
}); 

然後通過它運行你的分形結果: $results = collect($fractalResults)->fractal('camel_case')->get('data', []);