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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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



Hello Developers & Coders ,



My question is How to define a global variable , that can be used in
all controllers in Laravel ?



I have defined one variable $company in AppServiceProviders's boot method - that im using in all blade views , but I can not use it in controllers file , it gives error , undefined variable $company


$company


undefined variable $company


class AppServiceProvider extends ServiceProvider

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()

View::share('key', 'value');
Schema::defaultStringLength(191);

$company=DB::table('company')->where('id',1)->first();
View::share('company',$company);



/**
* Register any application services.
*
* @return void
*/
public function register()

//





please guide me , thanks for your efforts & time :)





if it is static you can use .env file or config file that you create. But if it is dynamic variable you can use session or cache to use it from everywhere.
– Erkan Özkök
Aug 11 at 10:35





@ErkanÖzkök , as you see in the post , I am using query builder to fetch record from database , the variable is dynamic not static.
– Saurabh Mistry
Aug 11 at 10:45




3 Answers
3



set configuration variables at runtime


class AppServiceProvider extends ServiceProvider

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()

View::share('key', 'value');
Schema::defaultStringLength(191);

$company=DB::table('company')->where('id',1)->first();
// View::share('company',$company);
config(['yourconfig.company' => $company]);




usage:


config('yourconfig.company');





thanks for your efforts , your answer is very helpful to me , so i m mark your answer as accepted and upvoted , :)
– Saurabh Mistry
Aug 11 at 12:29



Set a variable to session


Session::set('company', $company);



To get the variable when you use


$company = Session::get('company');





Thanks for your answer , but i dont want to use session , please suggest any other way , if it is possile :)
– Saurabh Mistry
Aug 11 at 11:56



Okay, so unless you want to keep it in your Session, which I absolutely do not recommend, Cache which does not seem to be a best idea either or set it through the Config system inside of a Framework (which already are 3 different solutions suited for different matters) I would start from thinking what that variable will contain, if that's something that's just a Collection of Company model then you can basically use it in any controller by just using Laravel Eloquent methods.



What I recommend is either $company = Company::where('foo', 'bar')->first();, or just some data provider that would return all the information that you need in the form of Laravel Collection.


$company = Company::where('foo', 'bar')->first();



tl;dr


#recommended way that's reusable throughout whole app
Company::find(1); // instead of DB::table('company')->where('id', 1)->get();



Hope that helps.





Company::find(1); and DB::table('company')->where('id', 1)->get(); . both are same , one is eloquent and one is query builer way , use any way but I want a glabally variable across all controllers
– Saurabh Mistry
Aug 11 at 11:53


Company::find(1);


DB::table('company')->where('id', 1)->get();





I'm not sure if using global variable is particularly great practice, and config should be used for configurational variables (thus the name) therefore I still think that unless you want to keep this kind of data inside of Redis, solution I proposed is best one for given problem.
– Kuba Biały
Aug 12 at 10:52






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS