Laravel has its own class to manage the session using the file system, redis, array, cookie and database. Session are used to keep the information about the user across the multiple request. As we know PHP is a server side language and all information lost after the response, so keep the information of user we need sessions. Laravel has drivers to use the session with database, file system, reds etc.
Configuration of session in Laravel
All configuration of session in Laravel stored in config/session.php file where we can define session driver, secure, path, database configuration, session time etc.
<?php
use Illuminate\Support\Str;
return [
/*
|--------------------------------------------------------------------------
| Default Session Driver
|--------------------------------------------------------------------------
|
| This option controls the default session "driver" that will be used on
| requests. By default, we will use the lightweight native driver but
| you may specify any of the other wonderful drivers provided here.
|
| Supported: "file", "cookie", "database", "apc",
| "memcached", "redis", "dynamodb", "array"
|
*/
'driver' => env('SESSION_DRIVER', 'file'),
.......
];
If you want to configure session using the database then you will need to create table for session in database. Laravel provides artisan command to generate the table or you can create the same table as below
Schema::create('sessions', function ($table) {
$table->string('id')->primary();
$table->foreignId('user_id')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->text('payload');
$table->integer('last_activity')->index();
});
Commands to migrate
php artisan session:table
php artisan migrate
Initializing session in Laravel
Session in laravel initialized by web middleware in kernel.php
and the name of class is StartSession
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
...
\Illuminate\Session\Middleware\StartSession::class,
..
],
}
Set session variable in laravel
There is two way to set and get the data in session.
- Set/Get session globally using helper function
session()
- Set/Get session using
$request
service in controller
Set session using $request set method, set method accepts two arguments first is key and secound is default value
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function show(Request $request, $id)
{
$value = $request->session()->set('key');
}
}
Or set session using global helper
$value = session('key', 'default');
Get session value in Laravel
Same as set session we can use $request or session() function to get the session data.
$value = session('key');
or using $request we can put method, put method accepts 2 arguments one is key and secound is value
$value = $request->session()->put('key','value');
We can also set multiple value using session() function
$value = session(['key'=>'value','key2'=>'value2']);
Get all session data
To get all stored value in session
$Alldata = $request->session()->all();
Delete Session data
If we would like to delete the session key then we can use forget method with an argument key and if we want to delete all session items from user session then we can use flush() method
// forget single item
$request->session()->forget('key');
// Forget multiple keys...
$request->session()->forget(['key', 'key2']);
// Forget all items from session
$request->session()->flush();