3

I have a model factory like this

$factory->define(App\Sale::class, function (Faker\Generator $faker) {
return [
    'unit' => $faker->randomDigit,
    'street_no' => $faker->randomDigit,
    'street_name' => $faker->streetName,
    'street_type' => $faker->streetSuffix,
    'suburb' => $faker->randomElements(['Melton South','Melton West','Rye']),
    'postcode' => $faker->numberBetween($min=1000, $max=4000),
    'sale_date' => $faker->dateTimeThisYear,
];
});

Database seeder runs it

factory(App\Sale::class, 5)->create();

The problem is when i run it php artisan db:seed I'm getting the error

  [Illuminate\Database\QueryException]
  Array to string conversion (SQL: insert into `sales` (`unit`, `street_no`,
  `street_name`, `street_type`, `suburb`, `postcode`, `sale_date`, 
`updated_at`, `created_at`) 
values (7, 1, Labadie Centers, Bridge, Rye, 3758, 2016-08-12 1
  5:02:07, 2017-05-23 13:16:56, 2017-05-23 13:16:56))

The error sql doesnt show any arrays that i can see.

When i pasted that sql into my db app and ran it, in order to make it work i had to quote all the strings, but laravel docs dont say anything about that when using faker ? Am I missing something in the model factory ?

exi

1
  • 9
    to whoever downvoted, congratulations on your ego Commented May 23, 2017 at 13:42

1 Answer 1

14

Try to change

'suburb' => $faker->randomElements(['Melton South','Melton West','Rye']),

to

'suburb' => $faker->randomElement(['Melton South','Melton West','Rye']),

Note 's' on Elements

Sign up to request clarification or add additional context in comments.

1 Comment

omg thankyou i was staring at your answer for a minute thinking 'whats the difference?' haha what a miss. will accept in a few mins when it lets me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.