Support the ongoing development of Laravel.io →
Laravel Views Blade

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
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.