ErrorException array to string conversion on migrate --seed

Julio Villalba picture Julio Villalba · Jul 17, 2015 · Viewed 11.6k times · Source

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

Answer

skrilled picture skrilled · Jul 17, 2015

$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.