QueryException"数组到字符串转换"当在laravel播种5.4

时间:2018-03-03 21:13:42

标签: database laravel faker laravel-seeding laravel-5.6

当我尝试使用php artisan db:seed播种数据库时 发生以下异常

  

数组转换为字符串(SQL:插入usersnameemailpasswordremember_tokenverified,{{ 1}},verification_tokenadminupdated_at)值(Rosanna Nicolas,kuhn.wilhelm @ example.net,$ 2y $ 10 $ bW.zAFI2rZaLSUKIsqoPLu24nH     otRIHRQkXYyKu8QwdcWRaOzblsC,l6ERPG47fC,1,...,0,2018年3月3日20时40分07秒,2018年3月3日20时40分07秒))

这是我的ModelFactory.php文件

created_at

这是我的迁移代码

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

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

1 个答案:

答案 0 :(得分:1)

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