简体   繁体   中英

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 ?

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 ?

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