2

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')),
];
});
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Julio Villalba
  • 149
  • 1
  • 2
  • 17

3 Answers3

10

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

skrilled
  • 5,350
  • 2
  • 26
  • 48
5

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.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Jayanthkumar S
  • 123
  • 3
  • 8
1

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

infomasud
  • 2,263
  • 1
  • 18
  • 12