laravel - Laravel Illuminate 不存储条目
问题描述
我正在使用 Laravel-5.8 和 Guzzle 获取外部使用 cron 作业来更新字段或在本地数据库中创建新字段。
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use Exception;
use Notification;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
class UpdateCreateEmployee extends Command
{
protected $signature = 'updatecreateemployees';
protected $description = 'Update or Create Employee Profile';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$client = new Client();
$res = $client->request('GET','https://api.myapi.net/laravelapp/myemployees', [
'query' => ['key' => 'jkklmssetyyy']
])->getBody();
DB::beginTransaction();
try {
$clientdatas = json_decode($res->getContents(), true);
foreach($clientdatas as $clientdata)
{
$payloads = [
'username' => strtok($clientdata['email_company'], '@'),
'first_name' => $clientdata['first_name'],
'last_name' => $clientdata['last_name'],
'other_name' => $clientdata['middle_name'],
'employee_type_code' => $clientdata['resource_type'],
];
if(!isset($clientdata['staff_id'])) {
$payloads['nav_dept_id'] = $clientdata['department_id'];
}
$employee = HrEmployee::updateOrCreate([
'employee_code' => $clientdata['staff_id'],
'email' => $clientdata['email'],
], $payloads);
$user = User::updateOrCreate([
'employee_code' => $employee->employee_code,
'email' => $employee->email,
],
[
'username' => $employee->username,
'password' => bcrypt("123456"),
'first_name' => $employee->first_name,
'last_name' => $employee->last_name,
]);
if (! $user->hasRole('Employee')) {
$user->assignRole('Employee');
}
$employee->update(['user_id' => $user->id]);
}
DB::commit();
} catch (Exception $exception) {
Log::error($exception);
DB::rollback();
}
}
}
从外部 api 中,一些员工更新了他们的 resource_type。
但是当我运行 cron 作业时,数据库中的 resource_type 没有更新。
当我检查错误日志时,我得到了这个:
Next Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'davidjones@useremail.com' for key 'email' (SQL: insert into
hr_employees
我该如何解决这个问题?
谢谢
解决方案
推荐阅读
- r - R:检测频率
- mysql - 如何使用 phpMyAdmin 中的“每组最大”查询修复错误?
- swift - SKTexture:加载图像资源时出错:“Tile_-4412407809”
- swift - 如何在 Swift 中的句子中获取特定关键字之前的单词?
- javascript - 给定一个 Map 如何按值排序以及值是否匹配按键 Javascript 排序?
- python - 如何在字典列表中拆分字典字符串值以添加新的字典项?
- google-chrome - 在 /userlicenses 上出现 500 后端错误
- hive - 向 hive 中的 case 语句添加子查询
- c# - 如何解决这个 for 循环正在冻结我的程序
- c# - 有两个重叠的控件接收相同的点击事件吗?