简体   繁体   中英

Laravel Use Eloquent model from custom UserServiceProvider

I'm new to Laravel, just toying with it and getting my head back into MVC.

I'm trying to make my own User auth provider (custom password hashing) as a service that implements the UserProviderInterface within Laravel.

Inside app/controllers/Account.php:

public function postCreate() {
    Auth::attempt(Input::all());
}

I have my app routing Auth::attempt through my custom provider class, and passing me the Input::all from the form into a retrieveByCredentials method.

Inside app/services/PasswordHash/PasswordHashUserProvider.php:

public function retrieveByCredentials(array $credentials) {
    // Why can't I do this?
    //Error: PasswordHash/User not found
    User::find($credentials['username']);

    dd($credentials);
}

I am lost at this point on how to access my User eloquent models from within this service class. I tried namespaces but had no luck.

The boot method on service providers use the service container to inject dependencies. To that end, you should be able to do the following (not using Facades, but I don't use facades that often).

class PasswordHashUserProvider extends ServiceProvider
{
    protected $user;

    public function boot(User $user)
    {
        $this->user = $user;
    }

}

You can then access user via $this->user

Source: https://laravel.com/docs/master/providers#the-boot-method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM