samedi 24 juin 2017

Laravel Seeding error SQLSTATE[23000]

When I'm trying to seed my database, but I get an error saying that I have a duplicate entry '1' for the key 'users_class_id_unique.

Does anyone know what is causing this problem?

This is what the error looks like

[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' 
for key 'users_class_id_unique' (SQL: insert into `users` (`name`, 
`lastname`, `email`, `class_id`, `password`
`role_id`, `created_at`, `updated_at`) values (Example, Name, 
example@outlook.com, 1, 
$2y$10$gqFXkYt9LRoCH6LqsosEPOI/2t.VDoNzBgV3x/przh.1KiuJqvRwe, 2, 1971-06-20 
06:10:44, 1975-03-0
9 17:46:23))



[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' 
for key 'users_class_id_unique'

This is my seeder file

<?php

use Faker\Factory;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UsersTableSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    $faker = Faker\Factory::create();

    //Zeker weten dat de eerste user één admin is
    DB::table('users')->insert([
        'name' => 'Rainier',
        'lastname' => 'Laan',
        'email' => 'rainier.laan@home.nl',
        'class_id' => 1,
        'password' => bcrypt('welkom'),
        'role_id' => 2,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    DB::table('users')->insert([
        'name' => 'Collin',
        'lastname' => 'Nieuw Beerta',
        'email' => 'collinweetnietwelkeemail@hotmail.com',
        'class_id' => 1,
        'password' => bcrypt('welkom'),
        'role_id' => 1,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    DB::table('users')->insert([
        'name' => 'Godpieter',
        'lastname' => 'alwin',
        'email' => 'Godpieter@Allmightygodpieter.com',
        'class_id' => 1,
        'password' => bcrypt('welkom'),
        'role_id' => 2,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    // Onderste code maakt 8 random users aan met het wachtwoord 'Welkom'

    foreach(range(1,7) as $index) {
        DB::table('users')->insert([
            'name' => $faker->name,
            'lastname' => $faker->lastName,
            'email' => $faker->email,
            'class_id' => rand(1, 3),
            'password' => bcrypt('welkom'),
            'role_id' => 1,
            'created_at' => $faker->dateTime,
            'updated_at' => $faker->dateTime,
        ]);
    }
}
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2t3jVV3
via IFTTT

Aucun commentaire:

Enregistrer un commentaire