mpociot/teamwork

Call to a member function attachTeam() on null

kiiraklis94 opened this issue · 2 comments

First of all thanks for this awesome package. So what I'm trying to do is when a user registers, he will automatically be added to a default team (only one for every user).

To do that, I added an event after a user is created, and added it to AppServiceProvider.php. My code is as follows:

//Add newly created user to team
        User::created( function($user) {
            $team = new Team();
            $team->owner_id = Auth::id();
            $team->name = 'main';
            $team->save();

            $user = User::where('id', '=', Auth::id())->first();
            $user->attachTeam($team, true); //also tried $user->attachTeam($team) and $user->teams()->attach($team->id); 
        });

This gives me the following error though:

FatalThrowableError in AppServiceProvider.php line 36:
Call to a member function attachTeam() on null

Am I doing something wrong? Thanks in advance for the help.

It seems I was doing something wrong in regards to how I used events, which has nothing to do with teamwork. I just put similar code into my RegisterController and now it works fine.

You can close this.

Thanks again for the awesome package.

Glad you've figured it out yourself :)