0

I'm trying to seed the database. Following the User example I create a Emploi (Job) seeder but I'm having an error.

seeding file

$factory->define(App\Emploi::class, function (Faker\Generator $faker) {

    return [
        'JOBURL' => $faker->name,
        'SALARYMAX' => '$50,000',
        'SALARYMIN' => '$40,000',
        'SALARYTYPE' => 'annual',
        'NAME' => $faker->words(5),
        'POSITION' => $faker->words(4),
        'JOBREF' => str_random(10),
        'JOB_SUMMARY' => $faker->text,
        'tweeted' => false,
        'POSTDATE' => $faker->dateTime(),
        'EXPIRYDATE' => $faker->dateTime(),
        'slug' => str_random(10),
    ];
});

exception

[ErrorException]
  Array to string conversion

output

 [Illuminate\Database\QueryException]
  Array to string conversion 
  (SQL: insert into `emplois` 

  (`JOBURL`, `SALARYMAX`, 
  `SALARYMIN`, `SALARYTYPE`, `NAME`, `POSITION`, 
  `JOBREF`, 
  `JOB_SUMMARY`, 
  `tweeted`, `
  POSTDATE`, `EXPIRYDATE`, `slug`, 
  `updated_at`, `created_at`) 
  values 
  (Lawson Boyer II, $50,000,
  $40,000, annual, rerum, voluptates, 
  LCw8d67S8w, 
  Nulla qui corporis sequi. 
  Eum nostrum culpa ut culpa velit. 
  Molestiae cumque doloremque et ex., 
  0, 
  1972-11-09 12:00:07, 1997-10-04 09:08:17, 6FxxCHFus6,
  2017-05-28 03:18:52, 2017-05-2  8 03:18:52)

  )

databaseSeeder

factory(App\Emploi::class, 50)->create();
3
  • Check the output for $faker->words(). Commented May 28, 2017 at 3:35
  • @WesleyPeeters thanks I'm having that array('leri', 'rednar', 'pratpu'). Can you post an answer so that I can vote up your answer Commented May 28, 2017 at 3:41
  • There you go! Have a nice day! Commented May 28, 2017 at 3:46

2 Answers 2

2

The Faker library has a few methods that'll return an array instead of a string. What laravel is expecting here is a string. So, you could either use PHP's implode function to create a string, or swap out the entire $faker->words() method for $faker->sentence(). Here's the method signature:

sentence($nbWords = 6, $variableNbWords = true)

Cheers!

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

1 Comment

Thanks for mentionning the API too! Have a nice day!
2

try implode('',$faker->words(5)) instead of $faker->words(5) as laravel expects your value to be either numerical or string type.

Comments

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.