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

nattha's avatar

php artisan tinker : Undefined variable: factory on line 1

Hi everyone,

I am working on a project where I use Model Factory. Until now I've always generated random users like this. With no problem. But all of a sudden I get this error message in Artisan Tinker:

$factory(\App\Models\User::class)->create() PHP error: Undefined variable: factory on line 1

I've tried to remove all of my factories and replaced it with the stock User Factory: $factory->define(App\User::class, function (Faker\Generator $faker) { return [ 'name' => $faker->name, 'email' => $faker->email, 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10), ]; });

With no success..

What could it be?

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

Are you using $factory(...) or factory(...) in tinker? It should be the second one... ;)

Please or to participate in this conversation.