4

I am trying to change Laravel Jetstream's logic in order to join a default team at registration rather than create a personal team. I found the method that does this:

public function create(array $input)
{
    Validator::make($input, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => $this->passwordRules(),
        'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
    ])->validate();

    return DB::transaction(function () use ($input) {
        return tap(User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
        ]), function (User $user) {
            $user->currentTeam = Team::find(1); # SQLSTATE[42S22]: Column not found: 1054 Unknown column 'currentTeam' in 'field list'
            $user->save();
        });
    });
}

The issue here is that currentTeam seems to be an created after the database level and not a column in the Database itself. The documentation says that currentTeam returns the Team model but doesn't say how to update the current users team programmatically.

I can see I can manually use a DB query to insert a row into the team_user table but there must be a method to do this.

return DB::transaction(function () use ($input) {
        return tap(User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
        ]), function (User $user) {
            DB::table('team_user')->insert(['user_id' => $user->id, 'team_id' => 1, 'role' => 'rfn']);
            $user->current_team_id = 1;
        });
    });

Any help would be appreciated, I understand this will be reliant on this team always existing in the Database but further measures will be implemented so it cannot be removed.

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • Looking at the [`Fortify/CreateNewUserWithTeams.php`](https://github.com/laravel/jetstream/blob/b33a95dabf6ca8dc6b3c6fe3135a9645bf70095b/stubs/app/Actions/Fortify/CreateNewUserWithTeams.php#L49) file, the `createTeam` method is called, which force creates a team and attaches that relationship, instead of doing `ownedTeams()->save` you can use the [`$user->switchTeam`](https://github.com/laravel/jetstream/blob/b33a95dabf6ca8dc6b3c6fe3135a9645bf70095b/src/HasTeams.php#L41) method instead and pass in `Team::find(1)`, that should work looking at the code. – Kim Hallberg Jul 28 '21 at 09:49
  • If that doesn't work, I don't see why [attaching](https://laravel.com/docs/8.x/eloquent-relationships#attaching-detaching) the team to the users [`teams()`](https://github.com/laravel/jetstream/blob/b33a95dabf6ca8dc6b3c6fe3135a9645bf70095b/src/HasTeams.php#L81) wouldn't work, since it's as a belongsToMany relationship. – Kim Hallberg Jul 28 '21 at 09:59
  • Thanks dude! That makes sense --> I couldn't find where this was being done for some reason! @KimHallberg – Jaquarh Jul 28 '21 at 10:44
  • 1
    Did some research and posted an answer that works for me on a new local installation of Laravel with Jetstream. – Kim Hallberg Jul 28 '21 at 10:52

2 Answers2

4

After some tinkering with a local Laravel version, this is was I came up with. This assumes the team you wanna add the user to is already created somehow, possibly through a seeder, or created manually. The file you need to edit is app/Actions/Fortify/CreateNewUser.php, namely the createTeam method.

Instead of creating a new team, we want to grab a team from the database, attach it to the newly registered user and then make that now attached team the users current team.

protected function createTeam(User $user)
{
    $team = Team::first();

    $user->teams()->attach($team);

    $user->switchTeam($team);
}

The reason we first need to attach the team first is that switchTeam checks if a user belongs to that team if it doesn't it returns false and doesn't switch to the new team. By attaching the team first we make the user belong to that team before we can actually switch to it as the current team.

Kim Hallberg
  • 1,165
  • 1
  • 9
  • 18
2

you can assign role as well in above answer of @KimHallberg as below

$team = Team::first();
$user->teams()->attach($team, array('role' => 'editor'));
$user->switchTeam($team);