php – Laravel如何在查询结果中添加新字段

如何在每个项目中添加新字段,我使用了put()但它只添加了最后一项.

return self::where('latest', 1)
            ->where('competitionId',$competitionId)
            ->orderBy('won','desc')
            ->orderBy('teamName','asc')
            ->get(['teamName','played','won','lost','percentage', 'streak'])
            ->put('test', ['123', '345'])
            ->toJson();

结果:

{
"0": {"teamName": "A"},
"1": {"teamName": "B"},
"2": {"teamName": "C", "test": ['123', '345']},
}

预期产量:

{
"0": {"teamName": "A", "test": "qwerty"},
"1": {"teamName": "B", "test": "qwerty"},
"2": {"teamName": "C", "test": "qwerty"},
}

解决方法:

你可以使用map()

->map(function ($item) {
  $item['test'] = ['123', '345'];
  return $item;
});
上一篇:php – 如何在Woocommerce管理产品列表中添加/删除列


下一篇:Springboot中Jackson的操作