Skip to content
Advertisement

PHP Notice: Array to string conversion; Database Factories

I have a talent model which can have many educations which I want to populate using data factories. But populating using artisan tinker the education data cause “Array to string conversion”. From what I see, I am not giving an array to be converted into a string. Below are the Education’s model, migration & factory

Error Message

PHP Notice: Array to string conversion in C:/Core/.../vendor/laravel/framework/src/Illuminate/Support/Str.php on line 360

Received On Running This Statement

$talents->each( function($talent) { factory(AppEducation::class)->create(['talent_id' => $talent->id]); })

JavaScript
JavaScript
JavaScript

Below are the tinker commands I ran

JavaScript

The $talents->each( function($talent) { factory(AppEducation::class)->create(['talent_id' => $talent->id]); }) is the cause, but I don’t understand why

The same command with different class model works say for example

JavaScript
JavaScript
JavaScript
JavaScript

Advertisement

Answer

This question was initially posted from the Laracasts website forum and I’ve just received the solution.

The issue was in my EducationFactory:

JavaScript

$faker->words return an array of strings. The solution is to use $faker->sentence

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement