Setting and reading sessions
Create a HomeController:
php artisan make:controller --resource HomeController
Your route /home will point to the HomeController:
Route::get('/home',[HomeController::class, 'index']);
Now add a session to index:
public function __construct(){
$this->middleware('auth');
}
public function index(Request $request)
{
$request->session()->put(['christine'=>'hello christine']);
session(['peter'=>'hello peter']);
echo $request->session()->get('christine');
//return view('welcome');
}
Above are two ways of saving sessions. We are only returning the first one when visiting the /home URL.
Global session function deleting
Add a method to forget the session:
public function index(Request $request)
{
$request->session()->put(['christine'=>'hello christine']);
session(['peter'=>'hello peter']);
$request->session()->forget('christine');
return $request->session()->all();
//return view('welcome');
}
To delete all sessions use this:
$request->session()->flush();
Flashing data
This type of session will show user the session once (flash it to them) and then forget it.
$request->session()->flash('message','Post has been created');
return $request->session()->get('message');
Two more types are reflash and keep:
$request->session()->reflash();
$request->session()->keep('message');