【问题标题】:QueryException "Array To String Conversion" when seeding in laravel 5.4在laravel 5.4中播种时出现QueryException“数组到字符串转换”
【发布时间】:2018-08-11 20:35:15
【问题描述】:

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

数组到字符串的转换(SQL:插入usersnameemailpasswordremember_tokenverifiedverification_tokenadmincreated_at,@987654 @) 值(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);
}

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

【问题讨论】:

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


    【解决方案1】:

    首先,randomElements 函数返回一个数组,而不是字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 2017-03-22
      • 2016-07-08
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多