Sessions Flashcards
Where is your application’s session configuration file stored?
It is stored at config/session.php
What is Laravel’s default session driver?
database
Which are two primary ways of working with session data in Laravel?
The global session helper and via a Request instance.
How can you retrieve all the data in the session?
$data = $request->session()->all();
How can you retrieve a subset of the session data?
Either by using the only method, which will only fetch the requested items, or by using the except method, which will fetch everything except the listed items.
How can you determine if an item is present and not null in the session?
->session()->has('example')
If you want to check if an item is present in the session, even if it is null, which method should you use?
->session()->exists('example')
To determine if an item is not present in the session, what do you need to do?
->session()->missing('example')
How can you add a new value onto a session value that is an array?
->session()->push('user.teams', 'developers');
This would add developers as a team to the users table.
Which method will retrieve and delete an item from the session in a single statement?
->session()->pull('example')
How can you count an integer up or down within your session data?
$request->session()->increment('count'); $request->session()->increment('count', $incrementBy = 2); $request->session()->decrement('count'); $request->session()->decrement('count', $decrementBy = 2);
How can you store an item in the session that is only going to be there for the next request?
->session()->flash('status', 'Task was successful!');
After being accessed once flashed items will be deleted from the session. Flash data is primarily useful for short-lived status messages.
Which methods let you persist your flash data for an additional request?
->session()->reflash(); for everything or ->session()->keep[('example', 'exampletwo']); for specific items
How can you remove one or multiple items from the session?
->session()->forget('example'); ->session()->forget(['example', 'exampletwo']);
Or to just remove everything from the session:->session()->flush();
How can you regenerate the session ID and how can you do that plus remove all data from the session in a single statement?
To just regenerate the session ID use ->session()->regenerate();
If you also want to remove all data from the session while also regenerating the session ID, use ->session()->invalidate();