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

gavin_r's avatar

Jetstream - How to programmatically create a user and team

As part of a seeder I'd like to create a specific user. If I do something like this:

$user = new User();
$user->name = 'John Smith';
$user->email = '[email protected]';
$user->email_verified_at = now();
$user->password = '...';
$user->save();

... the user gets created but the associated Team does not. Is there a better way to do this - somehow can I call the same underlying logic that Jetstream/Fortify calls when a user is created? Where can I find that?

Thanks!

0 likes
4 replies
r3alst's avatar

@automica

Due to few breaking changes in latest JetStream. To create team use following snippet:

$team = $user->ownedTeams()->create([
    'name' => 'New Team',
    'personal_team' => true
]);
gavin_r's avatar

Thanks @automica! That works (assuming you add user_id and personal_team attributes). Gets me going for now. I'd also be interested in a more elegant approach where I can use the same logic that Jetstream uses - perhaps from the app\Actions\Fortify\CreateNewUser.php create function?

automica's avatar

@gavin_r you dont need to pass in the user_id as its part of the relationship between user and teams so should be populated automatically when you create the team.

Theres nothing stopping you from copying the methods out of the CreateNewUser Action if you want to base your seeder on that too.

Please or to participate in this conversation.