Как я могу добавить новое поле в каждый элемент, я использовал 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();
по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();
по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; });