Php Laravel-完整性约束违反:1062重复条目

Php Laravel-完整性约束违反:1062重复条目,php,laravel,laravel-5,Php,Laravel,Laravel 5,我已经为这个问题挣扎了很长一段时间了,我有点急切地想要一个解决方案,因为我不太擅长解决问题。无论如何,我正在尝试为我的数据库植入种子,我遵循了Jeffrey在论坛系列中的教程,他在一个线程中添加了一个slug。嗯,我正试图对我的帖子做同样的事情,但是当我在我的数据库中设置种子时,我得到了标题中所述的错误,是什么导致了这一错误 我在这里完成了迁移: public function up() { Schema::create('posts', function (Blueprint $tab

我已经为这个问题挣扎了很长一段时间了,我有点急切地想要一个解决方案,因为我不太擅长解决问题。无论如何,我正在尝试为我的数据库植入种子,我遵循了Jeffrey在论坛系列中的教程,他在一个线程中添加了一个slug。嗯,我正试图对我的帖子做同样的事情,但是当我在我的数据库中设置种子时,我得到了标题中所述的错误,是什么导致了这一错误

我在这里完成了迁移:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->Unsignedinteger('user_id');
        $table->Unsignedinteger('channel_id');
        $table->string('title');
        $table->longText('text');
        $table->string('slug')->unique();
        $table->timestamps();
    });
}
这就是我想用我的播种机做的:

public function run()
{
    $faker = Faker\Factory::create();
    $title = $faker->sentence;

    foreach(range(1, 30) as $index) {
        DB::table('posts')->insert([
            'user_id' => rand(1, 50),
            'channel_id' => rand(1,14),
            'title' => $faker->sentence,
            'slug' => str_slug($title),
            'text' => $faker->paragraph,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);

    }
}
为了简短起见,我将在图片中显示完整的错误代码,如下所示:

但问题是,在我的迁移中,我将slug设置为唯一的,在我的种子中,我将标题指定为与slug相等,这是无法实现的,因为slug是唯一的,但在教程中,他也做了同样的操作,我如何解决这个问题

提前谢谢


链接到教程:

您的
$title
仅设置为1x,在循环之外。因此,您正在尝试使用相同的slug创建30条记录,如果它们需要唯一,那么这当然会失败。将
标题
slug
创建切换到循环中,以确保它们在每次迭代中都是新的和唯一的:

public function run()
{
    $faker = Faker\Factory::create();

    foreach(range(1, 30) as $index) {

        $title = $faker->sentence;
        $slug = str_slug($title);

        DB::table('posts')->insert([
            'user_id' => rand(1, 50),
            'channel_id' => rand(1,14),
            'title' => $title,
            'slug' => $slug,
            'text' => $faker->paragraph,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);

    }
}
你应该试试这个:

public function run()
{
    $faker = Faker\Factory::create();

    foreach(range(1, 30) as $index) {

      $title = $faker->sentence;

      DB::table('posts')->insert([
          'user_id' => rand(1, 50),
          'channel_id' => rand(1,14),
          'title' => $faker->sentence,
          'slug' => str_slug($title),
          'text' => $faker->paragraph,
          'created_at' => Carbon::now(),
          'updated_at' => Carbon::now(),
      ]);

    }

}

迁移过程中有两个输入错误:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('user_id'); // here
        $table->unsignedInteger('channel_id'); // and here
        $table->string('title');
        $table->longText('text');
        $table->string('slug')->unique();
        $table->timestamps();
    });
}

然后,我建议您根据自己的需要使用。

在我的情况下,我在测试数据库中的行id重复,softDelete处于启用状态(即在设置时删除),这导致了此错误。在我的例子中,我添加了Model::withTrashed()->where(),以获得软删除的行。

。添加一个关于你做了什么以及为什么做的解释,将大大提高你答案的质量。你的代码做了什么而OP没有?你为什么认为它能解决这个问题?