WEB DEVELOPER SITE
TUTORIALS HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

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.

Example

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

app/Http/Controllers/SessionController.php.

app/Http/Controllers/SessionController.php

<?php 

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'); 

else 

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.

app/Http/routes.php

Route::get('session/get','SessionController@accessSessionData'); 

Route::get('session/set','SessionController@storeSessionData'); 

Route::get('session/remove','SessionController@deleteSessionData'); 

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

http://localhost:8000/session/set 

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