Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel session

How to set and get session in Laravel 8?

Aman Jain, November 8, 2021November 17, 2021

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.

  1. Set/Get session globally using helper function session()
  2. 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();

Related

Php Laravel laravelphpsession

Post navigation

Previous post
Next post

Related Posts

Php Laravel 11 Ajax Image Upload with form with example

Laravel 11 Ajax Image Upload with form with example

July 16, 2024July 16, 2024

In this tutorial, I will demonstrate how to implement Laravel 11 Ajax Image Upload with form. This can be achieved using Laravel’s file and storage providers. In our previous articles, such as How to Upload image in Laravel 11 ? we explained how to upload images without Ajax. However, today…

Read More
Php How to Delete Cookies in Laravel

How to Delete Cookies in Laravel ?

July 26, 2022July 27, 2022

In this article we will learn to delete cookies in laravel. In our last article we learnt to set and get cookies and how cookies are used to store the data in client computer, Cookie can hold tiny information in computer and can retrieve when required for same website. In…

Read More
Uncategorized Custom middleware in laravel

How to create custom middleware Laravel 8

October 15, 2021November 6, 2023

In this tutorial we will going to learn custom middleware Laravel 8. Middleware are used to create a layer between request and response of the http request. it filters or create a logic before the request serve to the controller and also filter or modify the response. we can also…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version