如何在laravel中优雅地自定义集合键名称

时间:2019-05-29 23:49:20

标签: php arrays laravel collections

  1. 例如,有一个这样的收藏
$phone_Id = $request->get('q');
$colors = Phone::find($phone_Id)->color->toArray();
dd($colors);
  1. 打印结果
array:2 [▼
  0 => array:6 [▼
    "id" => 8
    "name" => "red"
    "value" => "#fb6250"
    "created_at" => "2019-05-29 01:42:51"
    "updated_at" => "2019-05-29 01:42:51"
    "pivot" => array:4 [▼
      "phone_id" => 1
      "color_id" => 8
      "created_at" => "2019-05-29 01:42:51"
      "updated_at" => "2019-05-29 01:42:51"
    ]
  ]
  1 => array:6 [▼
    "id" => 11
    "name" => "blue"
    "value" => "#202020"
    "created_at" => "2019-05-29 01:42:51"
    "updated_at" => "2019-05-29 01:42:51"
    "pivot" => array:4 [▼
      "phone_id" => 1
      "color_id" => 11
      "created_at" => "2019-05-29 01:42:51"
      "updated_at" => "2019-05-29 01:42:51"
    ]
  ]
]
  1. 我想要的最终结果是一个像这样的数组
array:2 [▼
  0 => array:2 [▼
    "id" => 8
    "text" => "red"
  ]
  1 => array:2 [▼
    "id" => 11
    "text" => "blue"
  ]
]
  1. 所以我目前的做法是这样的。
$data = [];
foreach ($colors as $key => $val) {
    $data[$key]['id'] = $val->id;
    $data[$key]['text'] = $val->name;
}
dd($data);
  1. 打印结果
array:2 [▼
  0 => array:2 [▼
    "id" => 8
    "text" => "red"
  ]
  1 => array:2 [▼
    "id" => 11
    "text" => "blue"
  ]
]
  

我想知道laravel中对于这种需求是否有更好,更漂亮的实现方式?

3 个答案:

答案 0 :(得分:3)

使用->trasform(..)

$colors = Phone::find($phone_Id)->color;

$colors->transform(function ($color) {
    return [
        'id' => $color->id,
        'text' => $color->name,
    ];
});

编辑::感谢您提供正确的答案和支持,但我想指出的是,OP使用查询生成器的->get(..)并重命名了列,从而提供了一种更为优雅的解决方案:

$colors = Phone::find($phone_Id)->color()->get(['id', 'name as text'])->toArray();

答案 1 :(得分:0)

使用// From Previous View Controller override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "ShowSection" { let controller = segue.destination as! SectionsViewController controller.context = context let button = sender as! UIButton let section = photos[button.tag].place controller.selectedSection = section! // This passes on a String } } // Saving pins to the map if let annotationView = annotationView { annotationView.annotation = annotation let button = annotationView.rightCalloutAccessoryView as! if let index = photos.firstIndex(of: annotation as! Photo) { button.tag = index } // In the Sections View Controller (i.e. the second VC) func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if let sections = fetchedRC.sections { let currentSection = sections[section] return currentSection.numberOfObjects } else { return 0 } } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return selectedSection }

尝试这个。

select('id', 'name')

答案 2 :(得分:0)

Laravel集合具有->only()方法和->map(),可以像这样使用:

$colors = Phone::find($phone_Id)->color()->only(['id', 'name'])toArray();

$colors->map(function($color) {
    $color->text = $color->name;
    unset($color->text);
})

https://laravel.com/docs/5.8/collections#method-only

https://laravel.com/docs/5.8/collections#method-map