dhermann
184
22
Laravel

Getting User Facade within View Composer

Posted 5 months ago by dhermann

I've created the standard View Composer class and registered it:


namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function boot()
    {
        View::composer('*', function($view) {
            $year = \App\Year::where('is_current', '1')->first();
            $camper = \App\Camper::where('email', Auth::user()->email)->first();
            $view->with('year', $year);
            $view->with('logged_in', $camper);
        });

    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}```

But at that point, the Auth::user() method is null. Can I "delay" the rendering of the View Composer until after that facade is available, or is there a way to get that data from another object?

Thanks,

Dan 

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.