Back

Undefined variable with two View Composer


i am trying to access first_user_name which is defined in the app service provider using view()->composer , but when trying to access the variable getting a error of undefined variable.

The boot Method in app service provider :

  public function boot()
    {
        Schema::defaultStringLength(191);
        
      
        view()->composer('pages.index', function ($view) {
            $view->with([
                'user_first_name' => explode(' ',auth()->user()->name)[0]
            ]);
        });

        view()->composer(['pages.prices','pages.locate-us'], function ($view) {
            $view->with([
                'locations' => Location::all(),
                'addons' => Addon::all()
            ]);
        });
    }

and the Navbar where i am trying to access the variable :

  @if(auth()->check())                    
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                               Hi, {{user_first_name}}
                            </a>
                            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                <a class="dropdown-item" href="#">Action</a>
                                <a class="dropdown-item" href="#">Another action</a>
                                <div class="dropdown-divider"></div>
                                <a class="dropdown-item" href="#">Something else here</a>
                            </div>
                        </li>
                    @else
                        <li class="nav-item">
                            <a href="/login" class="nav-link">
                                Sign In
                            </a>
                        </li>
                    @endif

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us