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]); })

class Education extends Model
{
    protected $fillable = [
        'talent_id',
        'institution',
        'education_level',
        'other_education_level',
        'qualification_field',
        'start_date_month',
        'start_date_year',
        'end_date_month',
        'end_date_year',
    ];

    public function talent() {
        return $this->belongsTo(Talent::class);
    }
}
Schema::create('educations', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('talent_id')->index();
    $table->string('institution');
    $table->string('education_level');
    $table->string('other_education_level')->nullable();
    $table->string('qualification_field');
    $table->unsignedTinyInteger('start_date_month');
    $table->unsignedSmallInteger('start_date_year');
    $table->unsignedTinyInteger('end_date_month')->nullable();
    $table->unsignedSmallInteger('end_date_year')->nullable();
    $table->timestamps();
});
$factory->define(Education::class, function (Faker $faker) {
    return [
        'talent_id' => factory(AppTalent::class),
        'institution' => $faker->word,
        'education_level' => $faker->word,
        'other_education_level' => $faker->word,
        'qualification_field' => $faker->words,
        'start_date_month' => rand(1, 12),
        'start_date_year' => rand(1970, 2000),
        'end_date_month' => rand(1, 12),
        'end_date_year' => rand(1970, 2000),
    ];
});

Below are the tinker commands I ran

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

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

$talents->each( function($talent) { factory(AppWorkExperience::class)->create(['talent_id' => $talent->id]); })
class WorkExperience extends Model
{
    protected $fillable = [
        'talent_id',
        'title',
        'employment_type',
        'company',
        'start_date_month',
        'start_date_year',
        'end_date_month',
        'end_date_year',
        'description',
    ];

    public function talent() {
        return $this->belongsTo(Talent::class);
    }
}
Schema::create('work_experiences', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('talent_id')->index();
    $table->string('title');
    $table->string('employment_type');
    $table->string('company');
    $table->unsignedTinyInteger('start_date_month');
    $table->unsignedSmallInteger('start_date_year');
    $table->unsignedTinyInteger('end_date_month')->nullable();
    $table->unsignedSmallInteger('end_date_year')->nullable();
    $table->text('description');
    $table->timestamps();
});
$factory->define(WorkExperience::class, function (Faker $faker) {
    return [
        'talent_id' => factory(AppTalent::class),
        'title' => $faker->word,
        'employment_type' => $faker->word(['Internship', 'Part Time', 'Full Time']),
        'company' => $faker->word,
        'start_date_month' => rand(1, 12),
        'start_date_year' => rand(1970, 2000),
        'end_date_month' => rand(1, 12),
        'end_date_year' => rand(1970, 2000),
        'description' => $faker->paragraph,
    ];
});

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:

'qualification_field' => $faker->words

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

'qualification_field' => $faker->sentence
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement