artyak
5 months ago
730
5
Laravel

Laravel updateOrcreate missing value

Posted 5 months ago by artyak

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.

Please sign in or create an account to participate in this conversation.