0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LaravelのJetstreamで新規ユーザ登録時のデフォルトチームを指定する

Last updated at Posted at 2022-09-09

前提条件

  • Laravel8.x
  • Jetstream Team版導入済み

Teamを事前に複数作成しておいてください

以下を編集します。

app>Actions>Fortify>CreateNewUser.php

CreateNewUser.php
 protected function createTeam(User $user)
    {
         $user->ownedTeams()->save(Team::forceCreate([
             'user_id' => $user->id,
             'name' => explode(' ', $user->name, 2)[0]."'s Team",
             'personal_team' => true,
         ]));

    }

を以下のようにします。

CreateNewUser.php
 protected function createTeam(User $user)
    {
        // $user->ownedTeams()->save(Team::forceCreate([
        //     'user_id' => $user->id,
        //     'name' => explode(' ', $user->name, 2)[0]."'s Team",
        //     'personal_team' => true,
        // ]));

        //指定のチームに所属させる
        $team = Team::find(3);//findでID3を指定
        //以下はロールの指定。無くても動くかも。
        $user->teams()->attach($team, array('role' => 'editor'));
        $user->switchTeam($team);

    }

is_Nullを入れていないので、確実にteamのID3があることが前提となります。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?