Gareth
Gareth

Reputation: 61

Array to string conversion when trying to seed database with faker in laravel 5.3

specifically this error only occurs with this:

$faker->randomElements($array = array('long','short','shoulder length')),

Output from php artisan db:seed

    [Illuminate\Database\QueryException]                                                                  
  Array to string conversion (SQL: insert into `profiles` (`agency_id`, `name`, `bio`, `age`, `hair_ty  
  pe`, `updated_at`, `created_at`) values (1, Cathy, Placeat voluptas tenetur corrupti et., 44, short,  
   2016-11-08 15:09:00, 2016-11-08 15:09:00)) 

     [ErrorException]            
  Array to string conversion 

Is there any attributes that I need to add to make this work, the sql clearly shows the randomly generated value in the query exception, nothing in the laravel logs.

Seeder:

namespace App;

use App\Profile;

use Illuminate\Database\Seeder;

class ProfileTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {

        $faker = \Faker\Factory::create();

        foreach(range(1,50) as $index)
        {
            Profile::create([
                'agency_id'         =>  '1',
                'name'              =>  $faker->firstName($gender = 'female'),
                'bio'               =>  $faker->sentence(4),
                'age'               =>  $faker->numberBetween($min=19, $max=46),
                'hair_type'         =>  $faker->randomElements($array = array('long','short','shoulder length'))
       ]);
    }
  }
}

Upvotes: 3

Views: 4964

Answers (1)

Gareth
Gareth

Reputation: 61

@Mihailo - Thanks sometimes you just can't see for looking.

There are two definitions:

$faker->randomElements($array = array('a','b','c'), $count = 1) //array('c')
$faker->randomElement($array = array('a','b','c')) //b

I was using the former and expecting the result from the latter.

Thanks

Upvotes: 3

Related Questions