Laravel - Session

Sessions are utilized to store data about the client over the solicitations. Laravel gives different drivers like file, cookie, apc, array, Memcached, Redis, and database to handle with session information.

Storing Session Data

Data can be stored in session utilizing the put() technique. The put() method will take two arguments, the "key" and the "value".

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

Deleting Session Data

The forget() technique is utilized to erase a thing from the session. This method will take key as the argument.

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

Use flush() technique rather than forget() method to erase all session information. Utilize the pull() technique to recover information from session and erase it a short time later. The force() technique will likewise take key as the contention. The contrast between the forget() and the pull() method is that forget() technique won't restore the estimation of the session and pull() method will return it and erase that esteem from session.


Step 1 − Create a controller called SessionController by executing the following command.

php craftsman make:controller SessionController - plain 

Step 2 − After fruitful execution, you will get the following yield −

Step 3 − Copy the following code in a document at




namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 

use App\Http\Controllers\Controller; 

class SessionController extends Controller { 

public function accessSessionData(Request $request){ 

if($request->session()- >has('my_name')) 

reverberation $request->session()- >get('my_name'); 


reverberation 'No information in the session'; 


public function storeSessionData(Request $request){ 

$request->session()- >put('my_name','Virat Gandhi'); 

reverberation "Data has been added to session"; 


public function deleteSessionData(Request $request){ 

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

reverberation "Data has been expelled from session."; 



Step 4 − Add the following lines at app/Http/routes.php file.





Step 5 − Visit the following URL to set information in session.


Step 6 − The yield will show up as appeared in the following image.

Stage 7 − Visit the following URL to get information from session. http://localhost:8000/session/get Stage 8 − The yield will show up as appeared in the following picture. Stage 9 − Visit the following URL to expel session information. http://localhost:8000/session/expel