Laravel session

Laravel session

Instructor-svgAl-Mamun Sarkar
Jul 11 , 2020

In this lesson, I will show you how to store, retrieve, and delete data from the session.

Session:

Storing Data:

$request->session()->put('session_key', 'value');

Session::put('test', 'Hello');

session(['session_key' => 'value']);

 

Retrieving Data:

$value = $request->session()->get('session_key');

$value = $request->session()->get('session_key', 'default');

$value = session('session_key');

$value = session('session_key', 'default');

$data = $request->session()->all();


use Illuminate\Support\Facades\Session;

Session::get('test');

Session::all();

 

If An Item Exists:

if ($request->session()->has('session_key')) {
    //
}

if ($request->session()->exists('session_key')) {
    //
}


use Illuminate\Support\Facades\Session;

Session::has('test');

Session::exists('test');

 

Deleting Data:

// Forget a single key...
$request->session()->forget('session_key');

// Forget multiple keys...
$request->session()->forget(['session_key1', 'session_key2']);

// Remove all
$request->session()->flush();


Session::forget('session_key');
Session::forget(['session_key1', 'session_key2'])
Session::flush();

 

Flash Data:

$request->session()->flash('message', 'Your Message');

Session::flash('message', 'Your Flash Message');

 

Getting Flash Data:

$request->session()->get('message');

Session::get('message');

 

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram