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

wivaku's avatar

Laravel 8: seed users + Jetstream teams

I want to seed / programmatically add users and teams / roles. Documentation explains how to add users and teams using UI, but it was not super clear how to add them programmatically.

This is what I have now. Is that the recommended / acceptable approach?

        $users = collect([
            ['name' => 'Adam', 'email' => '[email protected]' , 'password' => Hash::make('password'), 'role' => 'admin'],
            ['name' => 'Barry', 'email' => '[email protected]' , 'password' => Hash::make('password'), 'role' => 'editor'],
            ['name' => 'Carroll', 'email' => '[email protected]' , 'password' => Hash::make('password'), 'role' => 'viewer'],
        ]);

        // create users (using only the User model properties)
        User::insert( $users->map( function($e) {
            return Arr::only($e, Schema::getColumnListing('users') );
        })->toArray() );

        // manually create personal team for each user?

        // create team that is owned by the first user
        User::find(1)->ownedTeams()->create([
            'name' => "team X",
            'personal_team' => false,
        ]);

        $team = Team::find(1);

        $users->each(function($e) use($team) {
            if (!$team->hasUserWithEmail($e['email'])) {
                $team->users()->attach( Jetstream::findUserByEmailOrFail($e['email']), ['role' => $e['role'] ]);
            }
        });

        // mark this team as the current one for all these users
        User::whereNull('current_team_id')->update(['current_team_id' => $team->id]);
0 likes
5 replies
gitwithravish's avatar

If you can use factories for this, then use the following code

User Factory

class UserFactory extends Factory
{
    protected $model = User::class;

    public function definition()
    {
        return [
            '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),
            'team_id' => Team::factory()->create()->id
        ];
    }
}

TeamFactory

class TeamFactory extends Factory
{
    protected $model = Team::class;

    public function definition()
    {
        return [
            'name' => $this->faker->name
        ];
    }
}
There are two ways to go about this.
// Populate`teams` table with 6 entries
$teams = Team::factory->count(6)->create();

// Populate `users` table with 30 entries
$users = User::factory->count(30)->create();


// Now you need to populate `team_users` table.
//  Assign 1 to 3 teams to each user (randomly)
$users->each(function ($user) use ($teams) { 
    $user->teams()->attach(
        $teams->random(rand(1, 3))->pluck('id')->toArray()
    ); 
});
1 like
wivaku's avatar

Was not planning to use factory, because I want to add real users and teams.

Factory is indeed useful for testing, but a couple of issues with what you wrote.

User::factory() can not use team_id, it is the Team::factory() that needs to set the user_id. Here it is suggested to create Team factory with User factory inside. That's good for personal teams, but not for creating teams using existing users.

Also, it does not define the role of the user in the team.

BTW Team::factory() also requires personal_team=>false

powerspike's avatar

Greetings, Found this via a google search, There is a much easier way to do this now (for anyone else ending up here via search).

\App\Models\User::factory(10)->withPersonalTeam()->create();

The user factory now has a "withPersonalTeam" option, and also "unverified" as well.

7 likes

Please or to participate in this conversation.