简体   繁体   中英

Laravel 5.2 - Share data with all views

i have a header-user menu logged, each user registered have a balance import, i would like show this balance in my header section user logged, i'm tryng to use VIEW SHARE, like this but it doesn't work well:

class AppServiceProvider extends ServiceProvider
{

    public function boot()
    {
        //its just a dummy data object.
        $balance = UserBalance::where('user_id', Auth::id())->first();

        // Sharing -  the error interested this section: 
        view()->share('balance', $balance->balance);
    }

}

MY ERROR

ErrorException in AppServiceProvider.php line 23: Trying to get property of non-object

line 23 : view()->share('balance', $balance->balance);

MENU USER SECTION (inside my layout for all views):

@if (Auth::guest())
          <li><a href="{{ url('login')}}">Accedi</a></li>
          <li class="item-custom"><a href="{{url('register')}}">Registrati</a></li>
           @else
           <li class="hello-user">
           Ciao {{Auth::user()->name}}
           </li>
           <li> Your Balance: {{$balance}}</li>
@endif

Thank you for your help!

It may not be the best answer, but have you considered storing the results in the Session?

The session can be checked and read by any view.

if ($request->session()->has('balance')) {
     $request->session()->get('balance', 0);
}

Check the user is logged in or not. If user not authenticate than Auth::user()->id can't be exists and you also need to check $balance . If the query return null than the error will be show.

ErrorException in AppServiceProvider.php line 23: Trying to get property of non-object

Try this:

class AppServiceProvider extends ServiceProvider
{

    public function boot()
    {
        if(Auth::check()){
           //its just a dummy data object.
           $balance = UserBalance::where('user_id', Auth::user()->id)->first();

            // Sharing -  the error interested this section: 
            view()->share('balance', (count($balance) > 0) ? $balance->balance : 0);
        }
    }

}

Your Auth::id() return null or it does not retrieve session id. Because Laravel session is handled using session middleware and all middleware is executed before AppserviceProvider. As a result it does not retrieve session id.You can use this code. Because view()->composer clouser will be executed when the view is composed. Then you can use session id

        public function boot()
        {
            $this->getBalance();
        }

        public function getBalance()
        {
            view()->composer('*', function ($view) {
                    $balance = UserBalance::where('user_id', Auth::id())->first();
                    View()->share('balance', $balance->balance);
            });

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM