There are two primary ways of working with session data in Laravel:
Via a request instance.
$request->session()->put('key', 'value');
Via the global helper.
session(['key' => 'value']);
It is used to push a new value onto a session value that is an array.
$request->session()->push('user.uploadProfilePic', true);
1) Via a Request instance
public function show(Request $request, $id)
{
$value = $request->session()->get('key');
}
//Specifying a default value...
$value = $request->session()->get('key', 'default');
$value = $request->session()->get('key', function () {
return 'default';
});
2) Via global session helper
$value = session('key');
// Specifying a default value...
$value = session('key', 'default');
$data = $request->session()->all();
$request->session()->has('users')
// Return true if exist and value is not null
$request->session()->exists('users')
// If value is null use exists() method.
// Return true if key exists.value doesn't matter here
store items in the session only for the next request.
$request->session()->flash('status', 'Update successful!');
$value = $request->session()->pull('key', 'default');
// Forget a single key...
$request->session()->forget('key');
// Forget multiple keys...
$request->session()->forget(['key1', 'key2']);
// remove all keys...
$request->session()->flush();