如果 api 结果已修改,如何更新 table 中的列
How to update column in table if api results have been modified
我的应用程序:我的应用程序允许用户预测下一个比赛日即将举行的所有足球比赛的比分。我从 API 获取这些数据并将即将到来的游戏存储在我的数据库中。这些即将推出的游戏的 status
为 Scheduled
。现在我想每隔几分钟 运行 一个 cronjob,检查这些匹配项的状态是否已更改为 in_play
或 finished
,如果是这种情况,我想更新我的 status field
在我的数据库中用于与 api 中的状态字段的正确匹配。
如何检查状态是否已更改并在我的数据库中修改正确的匹配项?我存储了一个 match_id
可以用于此吗?
我的代码:
updateStatus
工作
public function handle()
{
$this->updateStatus();
}
public function updateStatus() {
$this->getMatches();
// check if status has been changed from schedulded to 'in_play' or 'finished'
// update the match status of the right matches in my database
}
public function getMatches() {
$client = new Client();
$uri = 'http://api.football-data.org/v2/competitions/PL/matches/?matchday=12&season=2018&matches';
$header = ['headers' => ['X-Auth-Token' => 'My-token']];
$res = $client->get($uri, $header);
return json_decode($res->getBody()->getContents(), true);
}
getMatches
作业(此作业获取api数据并将其存储在数据库中)
public function handle()
{
$this->saveMatches();
}
public function saveMatches()
{
$matches = $this->getMatches();
collect($matches['matches'])
->each(function ($match, $key) {
$date = new DateTime($match['utcDate']);
Match::create([
'match_id' => $match['id'],
'homeTeam' => $match['homeTeam']['name'],
'awayTeam' => $match['awayTeam']['name'],
'status' => $match['status'],
'date' => $date->format('Y-m-d'),
'time' => $date->format('H:i'),
'matchday' => $match['matchday'],
'homeScore'=> $match['score']['fullTime']['homeTeam'],
'awayScore'=> $match['score']['fullTime']['awayTeam']
]);
});
}
public function getMatches()
{
$client = new Client();
$uri = 'http://api.football-data.org/v2/competitions/PL/matches/?matchday=12&season=2018&matches';
$header = ['headers' => ['X-Auth-Token' => 'My-token']];
$res = $client->get($uri, $header);
return json_decode($res->getBody()->getContents(), true);
}
您可能想要做的是在 Match 对象上使用 Laravel 的 updateOrCreate() 方法。唯一标识信息似乎是匹配 ID。如果这永远不会改变,那么当您循环遍历每个语句时,您可以这样做:
Match::updateOrCreate([
'id' => $match['id'],
],[
'homeTeam' => $match['homeTeam']['name'],
'awayTeam' => $match['awayTeam']['name'],
'status' => $match['status'],
'date' => $date->format('Y-m-d'),
'time' => $date->format('H:i'),
'matchday' => $match['matchday'],
'homeScore'=> $match['score']['fullTime']['homeTeam'],
'awayScore'=> $match['score']['fullTime']['awayTeam']
]);
它的作用是查找具有相同 ID 的现有匹配项。如果它已经存在,它将简单地用 API 提供的所有信息更新它,包括比赛状态和比分。如果它尚不存在,它将创建它并将其作为与所有提供的信息的新匹配项存储在数据库中。希望这对您有所帮助!
我的应用程序:我的应用程序允许用户预测下一个比赛日即将举行的所有足球比赛的比分。我从 API 获取这些数据并将即将到来的游戏存储在我的数据库中。这些即将推出的游戏的 status
为 Scheduled
。现在我想每隔几分钟 运行 一个 cronjob,检查这些匹配项的状态是否已更改为 in_play
或 finished
,如果是这种情况,我想更新我的 status field
在我的数据库中用于与 api 中的状态字段的正确匹配。
如何检查状态是否已更改并在我的数据库中修改正确的匹配项?我存储了一个 match_id
可以用于此吗?
我的代码:
updateStatus
工作
public function handle()
{
$this->updateStatus();
}
public function updateStatus() {
$this->getMatches();
// check if status has been changed from schedulded to 'in_play' or 'finished'
// update the match status of the right matches in my database
}
public function getMatches() {
$client = new Client();
$uri = 'http://api.football-data.org/v2/competitions/PL/matches/?matchday=12&season=2018&matches';
$header = ['headers' => ['X-Auth-Token' => 'My-token']];
$res = $client->get($uri, $header);
return json_decode($res->getBody()->getContents(), true);
}
getMatches
作业(此作业获取api数据并将其存储在数据库中)
public function handle()
{
$this->saveMatches();
}
public function saveMatches()
{
$matches = $this->getMatches();
collect($matches['matches'])
->each(function ($match, $key) {
$date = new DateTime($match['utcDate']);
Match::create([
'match_id' => $match['id'],
'homeTeam' => $match['homeTeam']['name'],
'awayTeam' => $match['awayTeam']['name'],
'status' => $match['status'],
'date' => $date->format('Y-m-d'),
'time' => $date->format('H:i'),
'matchday' => $match['matchday'],
'homeScore'=> $match['score']['fullTime']['homeTeam'],
'awayScore'=> $match['score']['fullTime']['awayTeam']
]);
});
}
public function getMatches()
{
$client = new Client();
$uri = 'http://api.football-data.org/v2/competitions/PL/matches/?matchday=12&season=2018&matches';
$header = ['headers' => ['X-Auth-Token' => 'My-token']];
$res = $client->get($uri, $header);
return json_decode($res->getBody()->getContents(), true);
}
您可能想要做的是在 Match 对象上使用 Laravel 的 updateOrCreate() 方法。唯一标识信息似乎是匹配 ID。如果这永远不会改变,那么当您循环遍历每个语句时,您可以这样做:
Match::updateOrCreate([
'id' => $match['id'],
],[
'homeTeam' => $match['homeTeam']['name'],
'awayTeam' => $match['awayTeam']['name'],
'status' => $match['status'],
'date' => $date->format('Y-m-d'),
'time' => $date->format('H:i'),
'matchday' => $match['matchday'],
'homeScore'=> $match['score']['fullTime']['homeTeam'],
'awayScore'=> $match['score']['fullTime']['awayTeam']
]);
它的作用是查找具有相同 ID 的现有匹配项。如果它已经存在,它将简单地用 API 提供的所有信息更新它,包括比赛状态和比分。如果它尚不存在,它将创建它并将其作为与所有提供的信息的新匹配项存储在数据库中。希望这对您有所帮助!