Home > Back-end >  Array to string conversion error when running laravel 9 faker
Array to string conversion error when running laravel 9 faker

Time:08-07

In my laravel 9 application, I was trying to create the following faker.

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Model>
 */
class MemberFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            "firstname" => $this->faker->name(),
            "lastname" => $this->faker->name(),
            "dateofbirth" => $this->faker->date('YYYY-MM-DD'),
            "summary" => $this->faker->words(100),
            "division_name" => $this->faker->name()
        ];
    }
}

but every time when I try to seed the data, I get the following error,

enter image description here

CodePudding user response:

  • word // 'aut'
  • words($nb = 3, $asText = false) // array('porro', 'sed', 'magni')
    words return an array
    If you want to use words as text you need to put 2nd param is true

CodePudding user response:

the issue is not on the factory code as far as Im concerned, look at the foreach function, you're trying to concat an array to the $result with .= , which is not possible, you should implode the array first before doing so.

$result .= (implode(' ', array_shift($replace)) ?? $result).$segment;

assuming $result is a string, and $segment is also a string

  • Related