Reputation: 189
I am using Laravel so every logged in user can has a cart so I want to access their cart in my master blade because it's on the header and show all cart products in every pages
so I use this in AppServiceProvider but I need to check if the user logged in it will show cart
public function boot()
{
view()->composer('app.shop.2.layouts.master', function($view) {
$data = \Auth::user()->cart()->get()->first()->products();
$view->with('data', $data);
});
}
so everything is ok until the user is logged in but when the user is not logged in it gives me this error :
Call to a member function cart() on null (View: F:\larav-pay\payment\resources\views\app\shop\2\index.blade.php)
Upvotes: 0
Views: 260
Reputation: 1295
You can use Auth::check()
to check whether the user is authenticated or not like this:
public function boot()
{
view()->composer('app.shop.2.layouts.master', function($view) {
if (Auth::check()){
$data = \Auth::user()->cart()->get()->first()->products();
$view->with('data', $data);
}
});
}
Upvotes: 1