首页 文章

Laravel 5.1在复制时创建或更新

提问于
浏览
18

在Laravel 5.1中,对于MySQL插入,我想查看记录是否已经存在并在重复时更新,或者如果不存在则创建新的 .

我已经搜索了那里的答案是旧的laravel版本 . 在其中一个老话题中,它说去年在核心中添加了一个新的 updateOrCreate() 方法 . 但是当我尝试这个时,我收到了错误:

Integrity constraint violation: 1062 Duplicate entry '1' for key 'app_id'

这是我使用的查询:

AppInfo::updateOrCreate(array(
    'app_id' => $postData['appId'],
    'contact_email' => $postData['contactEmail']
));

其中 app_id 是该表中的唯一外键,我想更新记录(如果存在)或创建新记录 . 我试过搜索5.1文档但找不到我需要的信息 . 有人可以在这里指导我吗...

5 回答

  • 5

    根据Eloquent模型方法的定义“updateOrCreate()”

    function updateOrCreate(array $ attributes,array $ values = []){}

    需要两个论点......

    • one是要在数据库中检查的属性(如果记录存在)

    • 秒是您要创建或更新的新属性值


    AppInfo::updateOrCreate(['app_id' => $postData['appId']],
                            ['contact_email' => $postData['contactEmail']]);
    
  • 3

    我正在回答这个问题因为我找不到任何与ON DUPLICATE KEY UPDATE相关的答案,虽然我使用 Laravel 5.4 . 如果您查看Laravel核心代码中的updateOrCreate方法,您将看到所有Laravel使用2个不同的查询后:一个用于更新,另一个用于create . 因此,有时您可以在数据库中获取重复数据 . 所以在某些情况下,编写这种原始查询会很有用:

    DB :: statement(“INSERT INTO table_name(col_1,col_2)VALUES(?,?)ON DUPLICATE KEY UPDATE col_1 = col_1 1”,([val_1,val_2]));

    希望它对某人有用 .

  • 3

    我创建了一个包,用于在重复键上插入MySQL .

    它可能对其他人有用:

    https://packagist.org/packages/yadakhov/insert-on-duplicate-key

    例:

    /**
     * Class User.
     */
    class User extends Model
    {
        use Yadakhov\InsertOnDuplicateKey;
        ...
    }
    
    // associative array must match column names
    $users = [
        ['id' => 1, 'email' => 'user1@email.com', 'name' => 'User One'],
        ['id' => 2, 'email' => 'user2@email.com', 'name' => 'User Two'],
        ['id' => 3, 'email' => 'user3@email.com', 'name' => 'User Three'],
    ];
    
    User::insertOnDuplicateKey($users);
    
  • 29

    要使用laravel函数 updateOrCreate ,您需要在表中自动 increment id .

    他们在做什么

    select id from your_table where your_attributes

    之后获得自动增量ID

    然后

    update your_table set your_values where field_id

  • 11

    将以下方法insertUpdate添加到模型中

    <?php
    
    namespace App;
    
    use Illuminate\Auth\Authenticatable;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Auth\Passwords\CanResetPassword;
    use Illuminate\Foundation\Auth\Access\Authorizable;
    use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
    use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
    use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
    
    class User extends Model implements AuthenticatableContract,
                                        AuthorizableContract,
                                        CanResetPasswordContract
    {
        use Authenticatable, Authorizable, CanResetPassword;
    
        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'users';
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = ['name', 'email', 'password'];
    
        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = ['password', 'remember_token'];
    
    
        public static function insertUpdate(array $attributes = [])
        {
            $model = new static($attributes);
    
            $model->fill($attributes);
    
            if ($model->usesTimestamps()) {
                $model->updateTimestamps();
            }
    
            $attributes = $model->getAttributes();
    
            $query = $model->newBaseQueryBuilder();
            $processor = $query->getProcessor();
            $grammar = $query->getGrammar();
    
            $table = $grammar->wrapTable($model->getTable());
            $keyName = $model->getKeyName();
            $columns = $grammar->columnize(array_keys($attributes));
            $insertValues = $grammar->parameterize($attributes);
    
            $updateValues = [];
    
            if ($model->primaryKey !== null) {
                $updateValues[] = "{$grammar->wrap($keyName)} = LAST_INSERT_ID({$keyName})";
            }
    
            foreach ($attributes as $k => $v) {
                $updateValues[] = sprintf("%s = '%s'", $grammar->wrap($k), $v);
            }
    
            $updateValues = join(',', $updateValues);
    
            $sql = "insert into {$table} ({$columns}) values ({$insertValues}) on duplicate key update {$updateValues}";
    
            $id = $processor->processInsertGetId($query, $sql, array_values($attributes));
    
            $model->setAttribute($keyName, $id);
    
            return $model;
        }
    }
    

    您可以使用:

    App\User::insertUpdate([
        'name' => 'Marco Pedraza',
        'email' => 'mpdrza@gmail.com'
    ]);
    

    它将执行的下一个查询:

    insert into `users` (`name`, `email`, `updated_at`, `created_at`) values (?, ?, ?, ?) on duplicate key update `id` = LAST_INSERT_ID(id),`name` = 'Marco Pedraza',`email` = 'mpdrza@gmail.com',`updated_at` = '2016-11-02 01:30:05',`created_at` = '2016-11-02 01:30:05'
    

    如果已启用或禁用,该方法会自动添加/删除Eloquent时间戳 .

相关问题