Skip to content
Advertisement

Laravel returns Indirect modification of overloaded element of AppMatch has no effect when pushing new key

I’m having an issue when adding/pushing new key value to the result data, it return Indirect modification of overloaded element of AppMatch.

$results[$key][‘competitors’][1]->teamScore = $results[$key][‘competitors’][0]->scoreString;

Model Match method

public static function getTeamWinLossRecord($competitionId, $teamId) {
        $data = [];
        $results = self::select(['roundNumber', 'competitors'])
                ->where('competitionId', $competitionId)
                ->orderBy('roundNumber', 'desc')
                ->get()
                ->map(function($item){
                    $item['roundNumber'] = $item->roundNumber;
                    $item['competitors'] = json_decode($item->competitors);
                    return $item;
                });

        foreach ($results as $key => $matchArr) {
            if( $matchArr['competitors'][0]->competitorId == $teamId || $matchArr['competitors'][1]->competitorId == $teamId){
                if($matchArr['competitors'][0]->competitorId == $teamId){

                    $results[$key]['competitors'][1]->teamScore = $results[$key]['competitors'][0]->scoreString;
                    
                    array_push($data, ['Rnd '.$matchArr['roundNumber'], $results[$key]['competitors'][1]]);
                }
                else{
                    array_push($data, ['Rnd '.$matchArr['roundNumber'], $results[$key]['competitors'][0]]);
                }
            }
        }

        dd($data);
    }

Advertisement

Answer

Replace array push like this,

$data[] = array('Rnd '.$matchArr['roundNumber'], $results[$key]['competitors'][1]]);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement