lundi 7 mars 2016

Getting a service from app() doesn't always work in helper function

I am creating a helper function, and currently it looks like this:

function avatar($userid = null){
    return app()->User->getAvatar($userid);
}

The issue that I am having, is that I am getting an error that says:

Class User does not exist (View: /resources/views/layouts/usernav.blade.php)

This happens when I run this on some pages but not on other pages. I don't know what is causing the issue.

Blade Usage:

<img src="{{avatar()}}" id="avatar" style="height: 50px;" class="img-responsive">

We also have a service provider that looks like this:

class UserServiceProvider extends ServiceProvider {

    protected $defer = true;

    public function boot(User $user) {

    }

    public function register() {
        $this->app->resolving(User::class, function(User $user, $app){
            $user->setUser(Auth::user() ? Auth::user() : null);
            $this->app->instance('User', $user);
        });
    }

    public function provides(){
        return [User::class];
    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire