簡體   English   中英

遷移--seed時將ErrorException數組轉換為字符串

[英]ErrorException array to string conversion on migrate --seed

我正在嘗試建立我的第一個laravel項目,但是當我嘗試讓技術人員使用偽造者為數據庫播種時,它會拋出

[errorException]數組到字符串的轉換

我正在使用股票用戶遷移文件,並使用命令php artisan migration --seed

任何指導將不勝感激

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('role', array('user', 'admin', 'superuser'));
        $table->rememberToken();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('users');
}
}

和這個工匠為我生成的UserTableSeeder

use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    factory(App\User::class, 49)->create();
    factory(App\User::class)->create([
        'name' => 'admin',
        'role' => 'admin',
    ]);
}
}

這是我的Modelfactory.php

$factory->define(App\User::class, function ($faker) {
return [
    'name' => $faker->name,
    'email' => $faker->email,
    'password' => str_random(10),
    'remember_token' => str_random(10),
    'role' => $faker->word->randomElement(array('user','superuser')),
];
});

$table->string('role', array('user', 'admin', 'superuser'));

您正在選擇一種字符串類型,然后提供一個數組。

這正是您的錯誤所談論的。

您的錯誤是因為此行

$table->string('role', array('user', 'admin', 'superuser'));

字符串更改為枚舉 ; 例如:

$table->enum('role', array('user', 'admin', 'superuser'));

這將執行。

您說字符串,但在此行中提供一個數組:

 $table->string('role', array('user', 'admin', 'superuser'));

您應該使用:

$table->enum('role', ['user', 'admin', 'superuser']);

供參考,請參見此處:

https://laravel.com/docs/5.8/migrations#creating-columns

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM