在laravel 5.4中播种时,QueryException“数组到字符串转换”

问题描述 投票:1回答:1

当我尝试使用php artisan db:seed种子数据库后发生异常

阵列到字符串转换(SQL:插入usersnameemailpasswordremember_tokenverifiedverification_tokenadminupdated_atcreated_at)值(罗莎娜尼古拉斯,[email protected],$ 2Y $ 10 $ bW.zAFI2rZaLSUKIsqoPLu24nH otRIHRQkXYyKu8QwdcWRaOzblsC ,l6ERPG47fC,1,0,2018-03-03 20:40:07,2018-03-03 20:40:07))

这是我的ModelFactory.php文件

$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]),
];});

这是我的迁移代码

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();
    });
}

这是我的播种者课程

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();

}

这就是模型

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);
}

任何人都可以给出解决方案,将不胜感激。 !

database laravel faker laravel-seeding laravel-5.6
1个回答
1
投票

对于初学者来说,randomElements函数返回一个数组,而不是一个字符串。

© www.soinside.com 2019 - 2024. All rights reserved.