Sessions are used to store information about the user across the requests. Session can be configured in the file stored at config/session.php.
Sessions are a way to store data for each HTTP request with a unique session ID.
There are two methods to set the value in the session.
session helper function session().
use the Request instance to set the value in session.
// Global helper function
session(['key' => 'value']);
############## OR #########
// Request class instance
$request->session()->put(['key' => 'value']);
############## OR #########
// Pusing Value to Session Array
$request->session()->push(['key' => 'value']);
Import session in controller
use Illuminate\Support\Facades\Session;
use Session;
Accessing Session Data
To access the session data, we need an instance of session which can be accessed via HTTP request. After getting the instance, we can use the get() method, which will take one argument, “key”, to get the session data.
File name : index.php
// Global helper function
$value = session('key');
br/>
############## OR #########
$data = $request->session()->get('key');
############## OR #########
You can use all() method to get all session data instead of get() method
$data = $request->session()->all();
Storing Session Data
Data can be stored in session using the put() method. The put() method will take two arguments, the “key” and the “value”. or the session helper:
<script>
// your "Imaginary javascript"
window.location.href = '{{url("yoururl")}}';
// or
window.location.href = '{{route("myRoute")}}'; //using a named route
</script>