mpociot/teamwork

Call to undefined method App\User::attachTeam()

hondaman900 opened this issue · 3 comments

Thank you for this awesome package. I have installed it before and love it, but ran into a snag on a fresh installation.

Just did a fresh install in my Laravel 6 app. Migration and the php artisan make:teamwork scaffolding worked fine, and I can view the Create Team blade at myapp/teams/create. However, when I enter a new team name and click Save, I get this error:

BadMethodCallException
Call to undefined method App\User::attachTeam()

Seems to be coming from the TeamController store method line $request->user()->attachTeam($team);

I had this working with a Laravel 5.5 version of my app without issue.

Any suggestions as to what to do next?

Thanks in advance.

The issue is coming from the TeamController store function

public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string',
        ]);

        $teamModel = config('teamwork.team_model');

        $team = $teamModel::create([
            'name' => $request->name,
            'owner_id' => $request->user()->getKey(),
        ]);
        $request->user()->attachTeam($team);

        return redirect(route('teams.index'));
    }

The line $request->user()->attachTeam($team); breaks, but the code runs up to that point and the new team name created is stored in the database teams table by the lines directly above it in the function. The last step of assigning ownership of the team to the user bombs.

Is there a helper that provides attachTeam(), or where is that mapping to? I can't trace this issue and I'm dead in the water until I resolve this.

Any assistance is very much appreciated.

After two days of digging around in the code I found that my App\User.php was missing the following:

use Mpociot\Teamwork\Traits\UserHasTeams;

and the UserHasTeams part of

use Notifiable, UserHasTeams;

Correcting this resolved that issue and creating a team now works as expected. In previous Laravel 5.5 installations the make:teamwork scaffolding took care of this, updating the User model, but in this Laravel 6 app that was not done/missing.

I would usually close out this issue as I found my own solution, but am leaving this open in case there's still an issue with the package's scaffolding.

Thanks for your detailed report.

Adding the trait is part of the setup docs and was never part of the scaffolding (Laravel5.5 Compatible version https://github.com/mpociot/teamwork/blob/5.1.0/src/Mpociot/Teamwork/Commands/MakeTeamwork.php).

I close this Issue since its not a bug.