mpociot / teamwork

User to Team associations with invitation system for the Laravel 5 Framework
MIT License
1.07k stars 170 forks source link

Call to a member function attachTeam() on null #70

Closed kiiraklis94 closed 7 years ago

kiiraklis94 commented 7 years ago

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.

kiiraklis94 commented 7 years ago

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.

mpociot commented 7 years ago

Glad you've figured it out yourself :)