Setting a session variable in laravel

I would like to set a variable in a session using laravel this way

Session::set('variableName')=$value; 

but the problem is that I don’t know where to place this code, because I would like to install it at one time (when the guest views the home page or any other page)? The basic idea is to use a global variable to be used in all application controllers, I heard about something related to configuration variables, but I'm not sure if it would be a good idea to use configuration variables or just a session? Thanks

+14
php session-variables laravel laravel-routing
source share
8 answers

The correct syntax for this ...

 Session::set('variableName', $value); 

To get a variable you have to use ...

 Session::get('variableName'); 

If you need to install it once, I would find out exactly when you want it to be installed, and use "Events" for this. For example, if you want to install it when someone logs in, you will use ...

 Event::listen('auth.login', function() { Session::set('variableName', $value); }); 
+40
source share

I think your question can ultimately be reduced to the following:

Where can I set the long-term value available globally in my application?

The obvious answer is that it depends. Several factors depend on this:

  • Will the value ever be different or will it be the same for everyone?
  • How long does it last? (Forever? One day? One viewing session?)?

Config

If the value is the same for everyone and rarely changes, the best place will probably be indicated in the configuration file somewhere under app/config , for example. app/config/companyname.php :

 <?php return [ 'somevalue' => 10, ]; 

You can access this value from anywhere in the application through Config::get('companyname.somevalue')

Session

If the value that you are going to store will be different for each user, the most logical place to place it is Session . This is what you are talking about in your question, but using the wrong syntax. The correct syntax for storing a variable in a session is:

 Session::put('somekey', 'somevalue'); 

The correct syntax for returning it later is:

 Session::get('somekey'); 

How much when you perform these operations is a little up to you. I would choose a route filter if on Laravel 4.x or Middleware , if you use Laravel 5. The following is an example of using a route filter that uses a different class to actually come up with a value:

 // File: ValueMaker.php (saved in some folder that can be autoloaded) class ValueMaker { public function makeValue() { return 42; } } // File: app/filters.php is probably the best place Route::filter('set_value', function() { $valueMaker = app()->make('ValueMaker'); Session::put('somevalue', $valueMaker->makeValue()); }); // File: app/routes.php Route::group(['before' => 'set_value'], function() { // Value has already been 'made' by this point. return View::make('view') ->with('value', Session::get('somevalue')) ; }); 
+14
source share

in Laravel 5.4

use this method:

 Session::put('variableName', $value); 
+4
source share

In Laravel 5.6 you need to install it as

  session(['variableName'=>$value]); 

Getting it is as easy as

 $variableName = session('variableName') 
+2
source share

You can try

  Session::put('variable_Name', "Your Data Save Successfully !"); Session::get('variable_Name'); 
+1
source share

For example, to save data in a session, you will usually use the put method or the session helper:

 // Via a request instance... $request->session()->put('key', 'value'); 

or

 // Via the global helper... session(['key' => 'value']); 

to get an item from a session you can use get :

 $value = $request->session()->get('key', 'default value'); 

or session global assistant:

 $value = session('key', 'default value'); 

To determine if an element is present in a session, you can use the has method:

 if ($request->session()->has('users')) { // } 
+1
source share

To add to the answers above, make sure you define your function as follows:

 public function functionName(Request $request) { // } 

Pay attention to "(Request $ request)", now set up the session like this:

 $request->session()->put('key', 'value'); 

And get the session as follows:

 $data = $request->session()->get('key'); 

To erase a session, try this:

 $request->session()->forget('key'); 

or

 $request->session()->flush(); 
0
source share

What is the difference?

 Session::set Session::put 

or

 session(['key' => 'value']); 
0
source share

All Articles