Error: Call to a member function findBySlug() on string

cherisho - 1 month ago

Hello! This is my first project using the latest version of PyroCMS. I've used PyroCMS 2.2 in numerous projects before. Anyway, I have module where an administrator can create an "associate" account. Upon creation, we also need to create a corresponding user account with role "user". I have the following FormHandler: [code] class AssociateFormHandler { private $users = UserRepositoryInterface::class; private $roles = RoleRepositoryInterface::class; private $activator = UserActivator::class;

public function handle(AssociateFormBuilder $builder)
{
    if ($builder->canSave()) {

// dd('Test'); $first_name = ucfirst($builder->getFormValue('first_name')); $last_name = ucfirst($builder->getFormValue('last_name'));

        // TODO: Check if email address does not exist in user account DB

        $user = $this->roles->findBySlug('user');

        $this->users->unguard();

        $associate_user = $this->users->create(
            [
                'display_name' => $first_name . " " . $last_name,
                'email'        => $builder->getFormValue('email'),
                'password'     => $builder->getFormValue('password'),
                'username'     => strtolower($first_name.$last_name),
            ]
        );

        $associate_user->roles()->sync([$user->getId()]);

        $this->activator->force($associate_user);

        // Manually save the form
        $builder->setFormValue('user_id', $associate_user->getId());
        $builder->getFormEntry()->save();
    }

    return $builder;
}

} [/code]

With the debug enabled in .env file, I receive an error on this line: [code] $user = $this->roles->findBySlug('user'); [/code]

This is the error message: Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Call to a member function findBySlug() on string

Any idea why this happens, or what am I missing? I followed the example of creating a user in the UserSeeder.php of the Users Module. Thanks a lot in advance!