首页 > 解决方案 > 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 intohr_employees

我该如何解决这个问题?

谢谢

标签: laravel

解决方案


推荐阅读