Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

artyak's avatar

Laravel updateOrcreate missing value

I do parsing matches for DotA 2. Through a third-party API, I get current, future and past matches. But the problem how i understand, it's updateOrCreate function does not work. When parsing starts, it saves the values, but does not update the current ones. Here is an example of the answer that I get https://pastebin.com/KCcG6rJc, in this answer there is what teams are playing now, there are match_id of teams, if the teams are competing with each other, this value is the same. Using the example of this parsing result, I received the following record in the Database:

The maches table, the match_id value got there - 560004, that's right. But in the teams table, where these commands are indicated, match_id is null.

At my parser i has this code:

foreach ($live_matches as $live) {
   
    if ($live->tournament_id != $tournament->id) {
        continue;
    }
   
    $filtered_matches[] = $live;
   
    foreach ($teams as &$team) {
        if (false !== strpos($live->slug, $team->slug)) {
            $team->match_id = $live->id;
            $logo = $team->image_url;
            var_dump($team);
            exit;
        }
    }
}

var_dump($team); exit; say me:

object(stdClass)#1222 (8) {
  ["acronym"]=>
  string(8) "ThunderP"
  ["id"]=>
  int(2671)
  ["image_url"]=>
  string(93) "https://cdn.pandascore.co/images/team/image/2671/87033C79D6D1A70A66941BC8649EA5988D74582C.png"
  ["location"]=>
  string(2) "PE"
  ["modified_at"]=>
  string(20) "2020-04-30T01:51:51Z"
  ["name"]=>
  string(16) "Thunder Predator"
  ["slug"]=>
  string(16) "thunder-predator"
  ["match_id"]=>
  int(560004)
}

i.e. I get match_id, but at database it's not add.

And here is my full function, which update or create:

public function saveUpdate(int $tournament_id, array $tournament_teams, string $full_name, array $matches)
{
    $tournament = Tournament::updateOrCreate([
        'tournament_id' => $tournament_id,
        'league_name'   => $full_name
    ]);

    foreach ($matches as $match) {
        if ($match->status == 'not_started') {
            $status = 0;
        }
        if ($match->status == 'running') {
            $status = 1;
        }
        if ($match->status == 'finished') {
            $status = 2;
        }
        $team = Tournament_teams::where('team_id', $match->winner_id)->first();

        Tournament_matches::updateOrCreate([
            'name'       => $match->name,
            'started_at' => Carbon::parse("$match->begin_at,")->setTimezone('Europe/Berlin'),
            'ended_at'   => $match->end_at,
            'winner_id'  => $team->id ?? null,
            'status'     => $status,
            'match_id'   => $match->id,
            'live'       => $match->live_url,
        ]);
    }

    foreach ($tournament_teams as $team) {
        Tournament_teams::updateOrCreate([
            'tournaments_id' => $tournament->id,
            'team'           => $team->name,
            'slug'           => $team->slug,
            'logo'           => $team->image_url,
            'team_id'        => $team->id,
        ], [
            'match_id'       => $team->match_id ?? null,
        ]);
    }
}

For example, if I delete 1 column value from the teams table and start parsing, then the value in this column will not be created. That is, it creates data, but does not update.

And now teams with match_id 560334 are playing and the tournament_teams table doesn’t have such match_id, I searched for playing teams by name and found that there are 1 of the playing teams in the database 3 times in a row. That is, one record has match_id = null, the others of the same match_id commands have old ones. For some reason, the data on the current is not updated and empty values are received in the database, although 50% of the values are. Here is screenshot with highlighted commands for which for example data is not updated: Screenshot

Where could my mistake be? Why does var_dump pass me match_id, but this value does not get into the database? Where could my mistake be? I use Laravel 5.1. If you need to clarify any details - ask, thank you for your help.

0 likes
5 replies
Talinon's avatar

@artyak I just skimmed over your code, but the thing that jumped out at me is that you're not passing both parameters to updateOrCreate() in two places.

The method signature takes 2 parameters, the first being an array for what fields and values to match against, the second parameter being another array for what fields to update.

 $tournament = Tournament::updateOrCreate([
        'tournament_id' => $tournament_id,
        'league_name'   => $full_name
    ], [
                // missing this data
    ]);

Same here:

Tournament_matches::updateOrCreate([
            'name'       => $match->name,
            'started_at' => Carbon::parse("$match->begin_at,")->setTimezone('Europe/Berlin'),
            'ended_at'   => $match->end_at,
            'winner_id'  => $team->id ?? null,
            'status'     => $status,
            'match_id'   => $match->id,
            'live'       => $match->live_url,
        ]),
	[
			// missing this data
	]);


You probably want something like this:

Tournament_matches::updateOrCreate([
            'match_id'   => $match->id,
	], [
            'name'       => $match->name,
            'started_at' => Carbon::parse("$match->begin_at,")->setTimezone('Europe/Berlin'),
            'ended_at'   => $match->end_at,
            'winner_id'  => $team->id ?? null,
            'status'     => $status,
            'live'       => $match->live_url,
        ]);

artyak's avatar

At Tournament and Tournament_matches i received all data, and it's work, problem at Tournament_teams, here i use 2 parameters, am I transferring them correctly?

Talinon's avatar

@artyak Looks to me like you have them reversed.. check out the end of my edited response above

artyak's avatar

I make by your example, but a lot of data is lost at Tournament_teams i mean, i received only with match_id, where match_id null i don't received teams, but now 2 live matches, and i don't receive at my table 😢

Please or to participate in this conversation.