Fokrule
Fokrule

Reputation: 854

How to use same variable or query result in all the page in laravel

am using laravel 5.4, I want to use some query for all the page. Like I have one table to set permission for users. If users have permission then they will be able to get some extra menu. I have created a new page '_menu.blade.php'. This page is included to the all page. So, now what happening is, I have to check for all pages controller for users permission table and send those data. Suppose, I have home.blade.php. Here I have added menu.blade.php and then my controller for home.blade.php is

public function index(){
 $Permission  = Permission::where('edit',$currentUserId)->first();
return view('home')->withPermission($Permission);
}

And also I have read.blade.php. Here also I have added menu.blade.php and then my controller for read.blade.php is

public function index(){
 $Permission  = Permission::where('edit',$currentUserId)->first();
return view('read')->withPermission($Permission);
}

Here I have to fetch users permission for all the pages. Now What I want is, Just fetch user permission at once and use this permission in all the pages.

Upvotes: 0

Views: 399

Answers (1)

Brian Lee
Brian Lee

Reputation: 18187

You can share data with all views using:

public function boot()
{
    $Permission = Permission::where('edit',$currentUserId)->first();

    View::share('Permission', $Permission);
}

Add this to your AppServiceProvider.

Upvotes: 1

Related Questions