gpt4 book ai didi

database - 在 Laravel 5.4 中播种时出现 QueryException "Array To String Conversion"

转载 作者:搜寻专家 更新时间:2023-10-30 22:27:40 25 4
gpt4 key购买 nike

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

Array to string conversion (SQL: insert into users (name, email, password, remember_token, verified, verification_token, admin, updated_at, created_at) values (Rosanna Nicolas, kuhn.wilhelm@example.net, $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);
}

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

最佳答案

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

关于database - 在 Laravel 5.4 中播种时出现 QueryException "Array To String Conversion",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49089276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com