Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sbudisav's avatar

PHP Notice: Undefined variable: faker (laravel 8)

Postfactory.php

namespace Database\Factories;

use App\Models\Post; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str;

class PostFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ protected $model = Post::class;

/**
 * Define the model's default state.
 *
 * @return array
 */
public function definition()
{
    return [
        'title' => $this->faker->unique()->sentence,
        'body' => $this->faker->paragraph,
    ];
}

}

going into DatabaseSeeder.php and adding \App\Models\Post::factory(10)->create(); then running php artisan db:seed works just fine.

the user factory works just fine, I can not for the life of me figure out why the post factory does not work. Thanks

0 likes
2 replies
sbudisav's avatar
sbudisav
OP
Best Answer
Level 1

Figured it out, I feel foolish. I just needed to exit tinker inbetween updating the files

2 likes
comparado's avatar

OMG, thanks for sharing your solution - I did the same mistake and tried to fix it for hours :D

Please or to participate in this conversation.