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

cutigersfan's avatar

Laravel 8 Team::factory() not found

I'm using the following in my UserFactory:

    public function definition()
    {
        return [
            'first_name' => $this->faker->firstName,
            'last_name' => $this->faker->lastName,
//            'name' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
            'current_team_id' => Team::factory()
        ];


    }

When run the seeder, I get the following error:

   BadMethodCallException 

  Call to undefined method App\Models\Team::factory()

Any thoughts? This seems so silly, but it's frustrating me.

0 likes
8 replies
frankielee's avatar

factory(App\Models\Team::class)->create()->id instead?

1 like
cutigersfan's avatar

Well, the more I look at this it's somewhat circular. Teams asks for a user_id, so we'd need to commit the user first. THEN, users has a current_team_id that we'd need to update after the team is committed.

So, I'm not sure:

  1. How important this personal team stuff is to jetstream
  2. How to code around the circular reference. Guess I could simply skip the factory and simply put a loop around using the model classes and deal with the circular references.
frankielee's avatar

The current_team_id is nullable, Laravel will only update it only after you have login successfully. If you have previously selected Team and stored in the database, the team will be the default selected. You will need to create a team after the user is created.

  1. It's up to you. An example use case will be you have multiple projects which including different members, you will need it.

  2. Maybe you don't need the team module at all?

cutigersfan's avatar

Thanks for your thoughts. I do want a group & permissions feature and was going to attempt to use teams for that. I don't have current plans to allow for switching teams.

For now, I'm happy enough just to get some sample users in there. I'll move on for now.

connecteev's avatar

Same error here, and my use-case is a lot simpler with no circular reference.

<?php

namespace Database\Seeders;

use App\Role;
use App\User;
use Illuminate\Database\Seeder;
use Faker\Generator as Faker;

// Call using command: php artisan db:seed --class=AdminUsersTableSeeder
class AdminUsersTableSeeder extends Seeder
{
    public function run(Faker $faker)
    {
        $adminRole = Role::where([
            'title' => 'Admin',
        ])->firstOrFail();

        User::factory()->create([
            'id'                => 1,
            'email'             => env('ADMIN_EMAIL'),
            'username'          => env('ADMIN_USERNAME'),
            'password'          => Hash::make(env('ADMIN_PASSWORD')),
            'email_verified_at' => now(),
            'remember_token'    => Str::random(10),
            'created_at'        => now(),
            'updated_at'        => now(),
        ]);
    }
}

This is the output

$ php artisan db:seed --class=AdminUsersTableSeeder

   BadMethodCallException 

  Call to undefined method App\User::factory()

  at vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:50
     46▕      * @throws \BadMethodCallException
     47▕      */
     48▕     protected static function throwBadMethodCallException($method)
     49▕     {
  ➜  50▕         throw new BadMethodCallException(sprintf(
     51▕             'Call to undefined method %s::%s()', static::class, $method
     52▕         ));
     53▕     }
     54▕ }

  • Bad Method Call: Did you mean App\User::toArray() ? 

      +3 vendor frames 
  4   database/seeders/AdminUsersTableSeeder.php:20
      Illuminate\Database\Eloquent\Model::__callStatic("factory", [])

      +24 vendor frames 
  29  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

and it's driving me nuts. Anyone??

cutigersfan's avatar
cutigersfan
OP
Best Answer
Level 3

make sure your user class has the trait HasFactory:

use HasFactory;
4 likes
paccamicio's avatar

I applied it. But I receive this alert: Undefined type 'App\Models\HasFactory'.

Please or to participate in this conversation.