如何避免重复的记录进行插入

时间:2019-04-23 06:59:30

标签: php laravel eloquent laravel-5.6

我正在使用Laravel 5.6.29

if ( CharmFlag::where('post_id', '=', $postId)->where('charm_id', '=', $charm_id)->get()->isEmpty() ) {
      CharmFlag::create([
          'post_id' => $postId,
          'charm_id' => $charm_id,
          'gif_flag' => $gif_flag
      ]);
  } else {
      $charmFlag = CharmFlag::where('post_id', '=', $postId)->where('charm_id', '=', $charm_id)->first();
          $charmFlag->gif_flag = $gif_flag;
          $charmFlag->save();
     }

但是在数据库中我可以看到类似的记录 enter image description here

现在可以看到charm_id=18604charm_id=18605有重复的记录,但是根据我的编码,应该不会发生。

更新

也将代码更改为

$charmFlag = CharmFlag::firstOrNew(['post_id' => $postId, 'charm_id' => $charm_id]);
$charmFlag->gif_flag = $gif_flag;
$charmFlag->save();

但仍然得到重复的条目。

对此有趣的事实是对于每个重复的记录,时间戳也相同。同样,所有这些情况都只有第二条记录。

迁移

Schema::create('charm_flags', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('post_id');
    $table->unsignedInteger('charm_id');
    $table->boolean('gif_flag')->default(0);

    $table->timestamps();
    $table->foreign('post_id')->references('post_id')->on('posts');
    $table->foreign('charm_id')->references('charm_id')->on('base_charms');
});

更新2

更改为

CharmFlag::updateOrCreate(
          ['post_id' => $postId, 'charm_id' => $charm_id],
          [
              'gif_flag' => $gif_flag,
          ]
 );

但仍然得到重复的记录。

5 个答案:

答案 0 :(得分:6)

您不需要检查isEmpty,可以使用firstOrNew()updateOrCreate()方法

$charmFlag = CharmFlag::firstOrNew(['post_id' => $postId, 'charm_id' => $charm_id]);
$charmFlag->gif_flag = $gif_flag;
$charmFlag->save();

$charmFlag = CharmFlag::updateOrCreate(['post_id' => $postId, 'charm_id' => $charm_id]);
$charmFlag->gif_flag = $gif_flag;
$charmFlag->save();

请检查文件 https://laravel.com/docs/5.3/eloquent#insert-update-delete

答案 1 :(得分:3)

您可以尝试这样:

php-pdo

它还可以提高代码性能。

答案 2 :(得分:0)

创建重复项的机会处于第一个条件之内,->get()->isEmpty()似乎不起作用。

您能这样更新查询并查看吗?

if (CharmFlag::where('post_id', '=', $postId)->where('charm_id', '=', $charm_id)->first()) {

  CharmFlag::create([
      'post_id' => $postId,
      'charm_id' => $charm_id,
      'gif_flag' => $gif_flag
  ]);

} else {

  $charmFlag = CharmFlag::where('post_id', '=', $postId)->where('charm_id', '=', $charm_id)->first();
  $charmFlag->gif_flag = $gif_flag;
  $charmFlag->save();

  // you can also use below for update. this is faster and efficient.
  /* 
   CharmFlag::updateOrCreate(
    ['post_id' => $postId, 'charm_id' => $charm_id],
    ['gif_flag' => $gif_flag]
  );  
  */

}

答案 3 :(得分:0)

尝试:

$charmFlag = CharmFlag::where([['post_id',$postId],['charm_id',$charmId]])->first();

if($charmFlag)
    $charmFlag->gifFlag = $gif_flag;
else
{
    $charmFlag = new CharmFlag();
    $charmFlag->post_id = $postId;
    $charmFlag->charm_id = $charm_id;
    $charmFlag->gif_flag = $gif_flag;
}
$charmFlag->save();

答案 4 :(得分:0)

所有发布的答案都对我不起作用,因此基于对评论的建议

  1. 使用
  2. 删除了所有重复的条目

DELETE FROM charm_flags WHERE id IN (SELECT * FROM (SELECT MAX(n.id) FROM charm_flags n GROUP BY post_id, charm_id HAVING COUNT(*) > 1) x)

  1. 生成新的迁移以修改charm_flags表

Schema::table('charm_flags', function (Blueprint $table) { $table->unique(["post_id", "charm_id"]); });

如果有更好的解决方案,我想学习。