插入查询在我的本地工作正常但不在我的实时数据库上执行 (Laravel/MySQL)
Insert query works fine on my local but doesn't execute on my live database (Laravel/MySQL)
我有一个非常简单的函数,它循环遍历一个数组并将一些数据插入到结果中 table - 这在我的本地使用完全相同的代码工作得很好。在我使用 Laravel Valet 和 MySQL 数据库的本地设置 (Mac) 中,它命中函数 Result::create($data)
并将此数据插入数据库。然而,在 live/remote 站点上,由于某种原因,它从未在 insertUniqueMatches
中访问 Result::create()
。
我已经在 env 文件中添加了 db 用户,并且它已被授予所有权限,所以我不明白为什么这不会将条目插入到结果中 table。谁能解释我做错了什么?所有迁移都运行确保我的本地和实时数据库相同。
P.S 我已经尝试将 $fillable 变量与数组中的所有相关项目以及 $guarded 变量作为空白数组,但问题仍然存在。
class Result extends Model
{
use HasFactory;
// protected $fillable = ['match_id', 'home_team_id', 'away_team_id', 'home_team_goals', 'away_team_goals', 'outcome', 'match_date', 'properties', 'platform_id'];
protected $guarded = [];
public static function insertUniqueMatches($matches, $platform = null)
{
$inserted = 0;
foreach ($matches as $match) {
// check if existing match already exists in the db, if so don't re-insert this
if (Result::where('match_id', '=', $match['matchId'])->doesntExist()) {
$carbonDate = Carbon::now();
$carbonDate->timestamp($match['timestamp']);
$clubs = collect($match['clubs'])->values();
$data = [
'match_id' => $match['matchId'],
'home_team_id' => $clubs[0]['id'],
'away_team_id' => $clubs[1]['id'],
'home_team_goals' => $clubs[0]['goals'],
'away_team_goals' => $clubs[1]['goals'],
'outcome' => self::getMatchOutcome($clubs[0]),
'match_date' => $carbonDate->format('Y-m-d H:i:s'),
'properties' => json_encode([
'clubs' => $match['clubs'],
'players' => $match['players']
]),
'platform_id' => $platform
];
dump($data); // this shows valid data in the terminal
// this if condition is only reached on my local development but never on live so no inserts happen on the live DB
if (Result::create($data)) {
$inserted++;
dump('inserted matchId: '. $match['matchId']); // never see this on line but always on local
}
}
}
return $inserted;
}
我认为目前更好的解决方案是您可以找到问题所在。
您可以将代码写入 try-catch 以获取更多信息。
替换此代码
try {
Result::create($data);
} catch (\Exception $e) {
dd($e);
}
与:
dump($data); // this shows valid data in the terminal
// this if condition is only reached on my local development but never on live
if (Result::create($data)) {
$inserted++;
dump('inserted matchId: '. $match['matchId']); // never see this on line but always on local
}
我有一个非常简单的函数,它循环遍历一个数组并将一些数据插入到结果中 table - 这在我的本地使用完全相同的代码工作得很好。在我使用 Laravel Valet 和 MySQL 数据库的本地设置 (Mac) 中,它命中函数 Result::create($data)
并将此数据插入数据库。然而,在 live/remote 站点上,由于某种原因,它从未在 insertUniqueMatches
中访问 Result::create()
。
我已经在 env 文件中添加了 db 用户,并且它已被授予所有权限,所以我不明白为什么这不会将条目插入到结果中 table。谁能解释我做错了什么?所有迁移都运行确保我的本地和实时数据库相同。
P.S 我已经尝试将 $fillable 变量与数组中的所有相关项目以及 $guarded 变量作为空白数组,但问题仍然存在。
class Result extends Model
{
use HasFactory;
// protected $fillable = ['match_id', 'home_team_id', 'away_team_id', 'home_team_goals', 'away_team_goals', 'outcome', 'match_date', 'properties', 'platform_id'];
protected $guarded = [];
public static function insertUniqueMatches($matches, $platform = null)
{
$inserted = 0;
foreach ($matches as $match) {
// check if existing match already exists in the db, if so don't re-insert this
if (Result::where('match_id', '=', $match['matchId'])->doesntExist()) {
$carbonDate = Carbon::now();
$carbonDate->timestamp($match['timestamp']);
$clubs = collect($match['clubs'])->values();
$data = [
'match_id' => $match['matchId'],
'home_team_id' => $clubs[0]['id'],
'away_team_id' => $clubs[1]['id'],
'home_team_goals' => $clubs[0]['goals'],
'away_team_goals' => $clubs[1]['goals'],
'outcome' => self::getMatchOutcome($clubs[0]),
'match_date' => $carbonDate->format('Y-m-d H:i:s'),
'properties' => json_encode([
'clubs' => $match['clubs'],
'players' => $match['players']
]),
'platform_id' => $platform
];
dump($data); // this shows valid data in the terminal
// this if condition is only reached on my local development but never on live so no inserts happen on the live DB
if (Result::create($data)) {
$inserted++;
dump('inserted matchId: '. $match['matchId']); // never see this on line but always on local
}
}
}
return $inserted;
}
我认为目前更好的解决方案是您可以找到问题所在。 您可以将代码写入 try-catch 以获取更多信息。 替换此代码
try {
Result::create($data);
} catch (\Exception $e) {
dd($e);
}
与:
dump($data); // this shows valid data in the terminal
// this if condition is only reached on my local development but never on live
if (Result::create($data)) {
$inserted++;
dump('inserted matchId: '. $match['matchId']); // never see this on line but always on local
}