phplaravellaravel-seeding

"Array to string conversion" while seeding in Laravel 10


I keep getting the same error over and over again no matter what I try when I run the seeder. This is the code of the factory:

class ProjectFactory extends Factory
{
   
    public function definition(): array
    {
        $clients = Client::pluck('id');

        return [
            //
            'name' => fake()->sentence(),
            'description' => fake()->paragraphs(5),
            'start_date' => fake()->dateTimeInInterval('now', '+1 months'),
            'due_date' => fake()->dateTimeInInterval('+2 months', '+6 months'),
            'status' => fake()->randomElement(['new', 'in progress', 'cancelled', 'completed']),
            'notes' => fake()->paragraph(),
            'client_id' => fake()->randomElement($clients),

        ];
    }
}

this is the seeder:

class ProjectSeeder extends Seeder
{
   
    public function run(): void
    {
        //
        Project::factory()->count(25)->create();
    }
}

the ones without foreign are seeding fine, so I think that's the problem, but every solution I try gives me the same error.

enter image description here

help me please!


Solution

  • The fake()->paragraphs(5) method returns an array of paragraphs, not a single string but SQL requires string for the description field, hence the conversion error. To combat that you can simply implode() the array into string:

    class ProjectFactory extends Factory
    {
        public function definition(): array
        {
            $clients = Client::pluck('id')->all();
    
            return [
                // ...
                'description' => implode("\n\n", fake()->paragraphs(5)), 
                // ...
            ];
        }
    }