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

时间:2017-06-14 01:49:50

标签: php json laravel laravel-5 backend

如何在每个项目中添加新字段,我使用了 <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td background="[IMAGE GOES HERE]" style="background:url('[IMAGE GOES HERE]'); background-image: url('[IMAGE GOES HERE]');"> <!--[if gte mso 9]> <v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="width:[ADD WIDTH OF IMAGE];height:[ADD IMAGE HEIGHT];"> <v:fill type="frame" src="[IMAGE GOES HERE]" color="[FALLBACK COLOR]" /> <v:textbox inset="0,0,0,0"> <![endif]--> [HTML CONTENT HERE] <!--[if gte mso 9]> </v:textbox> </v:rect> <![endif]--> </td> </tr> </tbody> </table>,但它只添加了最后一项。

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']},
}

1 个答案:

答案 0 :(得分:10)

您可以使用map()

->map(function ($item) {
  $item['test'] = ['123', '345'];
  return $item;
});