Getting User Facade within View Composer

Posted 1 month 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.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.