简体   繁体   中英

ErrorException array to string conversion on migrate --seed

I'm trying to set up my very first laravel project however when I try to have artisan to seed the database with faker it throws

[errorException] array to string conversion

I'm just working with the stock users migration file and using the command php artisan migrate --seed

Any guidance would be greatly appreciated

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

and this UserTableSeeder that artisan generated for me

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

this is my 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'));

You are selecting a type of string and then providing an array.

This is exactly what your error is talking about.

Your error is because of this line

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

change string to enum ; ex:

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

this will execute.

You say string but provide an array in this line:

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

You should use :

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

For reference see here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM