Session : As we know that sessions are object that is used for temporary user’s request data .
In Laravel : Session data handle by the different drivers like apc, array, Memcached,file, cookie,Redis, and database
config/session.php – Where the Laravel session could be configured
Accessing Session Data :
In Laravel , To access the session data , First We need the session’s instance , which get via HTTP request.
After that , Laravel provides the get() methods , which contain one parameters that is called ‘key‘, to retrieve the session.
This is below example. How to access the session : –
$sessionValue = $request->session()->get('key');
If you want to retrieve all session data in Laravel , Then use all() methods.
Storing Session Data :
In Laravel – if you want to store session data then use put() method. It contains two parameters one is ‘key‘ and another is ‘value‘
This is below example . How to store session data :
$request->session()->put('key', 'value');
Deleting Session Data :
After Accessing Session Data and Storing Session Data in Laravel , Now it is time for Deleting Session Data. So laravel provide the forget() method , whic is used for delete the particular session data
. It contains the one parameters ‘key‘
This is below example , How to delete the session data :
$request->session()->forget('key');
Note : In Laravel , If you want to delete all session data then Use flush() method
Example :
Step 1 − We Create a controller ‘SessionController‘ by executing the following command
php artisan make:controller SessionController
Step 2 − This is ‘SessionController.php‘ folder structure :
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('name')) echo $request->session()->get('name'); else echo 'No data in session'; } public function storeSessionData(Request $request){ $request->session()->put('name','Outsource2global'); echo "Data added to session"; } public function deleteSessionData(Request $request){ $request->session()->forget('name'); echo "Data removed from session."; } } ?>