1

when I try to seed database using php artisan db:seed following exception is occured

Array to string conversion (SQL: insert into users (name, email, password, remember_token, verified, verification_token, admin, updated_at, created_at) values (Rosanna Nicolas, [email protected], $2y$10$bW.zAFI2rZaLSUKIsqoPLu24nH otRIHRQkXYyKu8QwdcWRaOzblsC, l6ERPG47fC, 1, , 0, 2018-03-03 20:40:07, 2018-03-03 20:40:07))

this is my ModelFactory.php file

$factory->define(User::class, function (Faker\Generator $faker) {
static $password;
return [
    'name' => $faker->name,
    'email' => $faker->unique()->safeEmail,
    'password' => $password ?: $password = bcrypt('secret'),
    'remember_token' => str_random(10),
    'verified' => $verified = $faker->randomElement([User::VERIFIED_USER,User::UNVERIFIED_USER]),
    'verification_token' => $verified == User::VERIFIED_USER ? null : User::generateVerificationCode(),
    'admin' => $verified = $faker->randomElements([User::ADMIN_USER, User::REGULAR_USER]),
];});

this is my migration code

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->string('verified')->default(User::UNVERIFIED_USER);
        $table->string('verification_token')->nullable();
        $table->string('admin')->default(User::REGULAR_USER);
        $table->timestamps();
    });
}

this is my seeder class

public function run()
{
    DB::statement('SET FOREIGN_KEY_CHECKS = 0');
    User::truncate();
    Category::truncate();
    Product::truncate();
    Transaction::truncate();
    DB::table('category_product') -> truncate();

    $usersQuantity = 200;
    $categoriesQuantity = 30;
    $productsQuantity = 1000;
    $transactionsQuantity = 1000;

    factory(User::class, $usersQuantity)->create();
    factory(Category::class, $categoriesQuantity)->create();

    factory(Product::class, $productsQuantity)->create()->each(
        function ($product) {
            $categories = Category::all()->random(mt_rand(1, 5))->pluck('id');

            $product->categories()->attach($categories);
        });
    factory(Transaction::class, $transactionsQuantity)->create();

}

and this is the model

class User extends Authenticatable{
use Notifiable;

const VERIFIED_USER = '1';
const UNVERIFIED_USER = '0';

const ADMIN_USER = '1';
const REGULAR_USER = '0';

protected $table = 'users';
protected $fillable = [
    'name',
    'email',
    'password',
    'verified',
    'verification_token',
    'admin',
];
 protected $hidden = [
    'password',
    'remember_token',
    'verification_token',
];
public function isVerified(){
    return $this->verified == User::VERIFIED_USER;
}
public function isAdmin(){
    return $this->admin == User::ADMIN_USER;
}
public static function generateVerificationCode(){
    return str_random(40);
}

anyone can give the solution it will be grateful. !

1 Answer 1

2

Well for starters the randomElements function returns an array, not a string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.