calebporzio/onboard

Unresolvable dependency resolving [Parameter #0 [ <required> $user ]] in class Calebporzio\Onboard\OnboardingManager

factormaarten opened this issue ยท 12 comments

Hi,

I'm getting an unresolvable dependencing resolving as somehow it's not passing on the $user class properly. I'm using spark and this is my User model:

<?php

namespace App;

use Laravel\Spark\CanJoinTeams;
use Laravel\Passport\HasApiTokens;
use Laravel\Spark\User as SparkUser;
use Calebporzio\Onboard\GetsOnboarded;

class User extends SparkUser
{
    use CanJoinTeams, HasApiTokens, GetsOnboarded;

Hey @mpjraaij what version of Laravel are you using?

@wilburpowery I've got the same error, using Laravel 5.6.15 and Spark 5.0.1. The error is triggered when calling Auth::user()->onboarding().

Type-hinting $user in OnboardingManager would fix the issue. How is the container supposed to resolve what $user refers to without hinting ?

When you call Auth::user()->onboarding() that function does the following:

public function onboarding()
{
   return App::make(OnboardingManager::class, [$this]);
}

It's sends through the user instance to the Manager class.

I'll test this out locally later today, maybe the make command has changed a bit in v5.6. I'll take a closer look for you.

Just out of curiosity, you are installing the dev-master version of the package right? Caleb hasn't made a release with the changes to support Laravel 5.6.

So, a few things about this:
We can't just typehint the user model on the Manager class bacause the container will just fetch a new instance of the class.

Since we have an argument that is not resolved from the container on the class, instead of using the App::make we can use App::makeWith() and we can pass the value for the $user argument from for the constructor. The problem is, that method is only available from version ^ 5.3

Just out of curiosity, you are installing the dev-master version of the package right? Caleb hasn't made a release with the changes to support Laravel 5.6.

Yes, I installed the dev-master version, but a tag for 5.6 would be nice.

[...] instead of using the App::make we can use App::makeWith() and we can pass the value for the $user argument from for the constructor. The problem is, that method is only available from version ^ 5.3

Ok, for now I will add the method to my user class instead of using the trait.

/**
 * Until the Calebporzio\Onboard\GetsOnboarded trait gets updated for Laravel 5.6.
 *
 * @see https://github.com/calebporzio/onboard/issues/5
 */
public function onboarding()
{
    return \App::makeWith(OnboardingManager::class, ['user' => $this]);
}

Yeah. This solves your issue right?
You might want to change it like this:

return \App::makeWith(OnboardingManager::class, ['user' => $this]);

Just to make it more clear that your passing the user argument. ๐Ÿค 

My change fixed the exception but the steps would not be marked as completed. Your change fixed that, I've updated my comment.

Great. Glad I could help man.
Hopefully Caleb sees this issue soon and releases a fix.

Thanks for pitching in here @wilburpowery

I'll try to dig into this soon

Sorry this took so long - merged