Vinc
Vinc

Reputation: 11

Global variable in laravel 5.5

I'm new to laravel and currently making a dashboard with laravel and stuck on how to store variable that can be accessed by all view and controller and can be changed by controller. For example i'm logging in into the dashboard and i want to store the query i got from the database .

$admin = DB::table('admin')->where('username',$request->user)->select('admin_name')->first();

After that i want $admin to be stored globally so all the view and controller can use it. I've tried session but it doesn't work and always return null value. Any anybody tell me another solution ?

Upvotes: 0

Views: 90

Answers (1)

Cloonix
Cloonix

Reputation: 113

Maybe you can explain what you intend to do.

You could use Laravel sessions:

session(['user' => User::first()]);

https://laravel.com/docs/5.5/session#session-usage

Also, a similar/same question was asked before:

Laravel 5.5 : How to define global variable that can be used in all controllers ?

Upvotes: 1

Related Questions