【问题标题】:How to elegantly customize the collection key name in laravellaravel 中如何优雅地自定义集合键名
【发布时间】:2019-10-15 14:50:00
【问题描述】:
  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中对于这种需求有没有更好更漂亮的实现?

【问题讨论】:

    标签: php arrays laravel collections


    【解决方案1】:

    使用->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();
    

    【讨论】:

    • 非常感谢,我想出了一个更简单的方法。 php $colors = Phone::find($phone_Id)->color()->get(['id', 'name as text'])->toArray(); 只需一段代码即可实现
    • 哦,不错,很优雅
    【解决方案2】:

    Laravel Collection 有一个 ->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

    【讨论】:

    • 您好,非常感谢, php $colors = Phone::find($phone_Id)->color->only(['id', 'name']); dd($colors); resule : shell Collection {#861 ▼ #items: [] }
    • 欢迎您@Angu。如果这是您正在寻找的答案,您可以通过选择勾号告诉人们它已得到回答。
    • 根据您的提示,我想出了最简单的方法。 $colors = Phone::find($phone_Id)->color()->get(['id', 'name as text'])->toArray();
    • 对不起,集合是空的,因为你必须把颜色写成 color()
    • 非常感谢,你的方法确实有效果。
    【解决方案3】:

    使用select('id', 'name')

    试试这个。

    $colors = Phone::find($phone_Id)->color()->select('id', 'name')->get()->toArray()
    
    dd($colors);
    

    【讨论】:

    • 我可能弄错了,但是她似乎在 find 结果上使用了关系,这意味着 ->select() 方法不可用
    • 错误方法 Illuminate\Database\Eloquent\Collection::select 不存在。
    • 很好,此代码可能适用于父模型。你可以使用上面stackoverflow.com/a/56370088/4551227 JOSH 的答案
    • @JesusErwinSuarez 方法 Illuminate\Database\Eloquent\Collection::select 不存在。因为color需要写成color()
    • 我明白了,谢谢。我刚刚意识到 color 和 color() color - variable color() - instance 如果我没记错的话。
    猜你喜欢
    • 2011-02-22
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多