Global variable in laravel 5.5

145
November 27, 2019, at 6:10 PM

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 ?

Answer 1

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 ?

READ ALSO
php, pdf and bad form feed [on hold]

php, pdf and bad form feed [on hold]

Goodmorning everyoneI'm using a php application that prints in pdf

97
limit array member dependent on different array and count

limit array member dependent on different array and count

I created a site by laravel and I want to show categories and relevant products names

82
How to avoide throwing exception if an answer is 404 with Guzzle?

How to avoide throwing exception if an answer is 404 with Guzzle?

Not result is result too in my caseBut Guzzle throws me an exception and as result, my promise has 'rejected' status

91
Why chrome not return to exist image if .webp not exist?

Why chrome not return to exist image if .webp not exist?

I am trying to servwebp extentions for supporting browsers

80