How to set and get session data in Laravel

  2 months ago     Views:  205  
Julius Braun
1 Follows


In a programming language, session is a great way to store data. It makes easy to handle data without storing in a database.

Session is useful to protect the data that the user wouldn't be able to read or write. Sessions are easier and secure cookies. Sessions are useful user logins, carts etc.

Laravel framework has easy ways to store and retrieve session. Laravel session can be stored in database, files or encrypted cookies. Laravel session configuration is stored in config/session.php file. By default, Laravel store session data in files. If you want to store session in database table, then change session driver to database and run bellow command to generate sessions table.

php artisan session:table

php artisan migrate

This will create session table in database where all session data is saved.

Store data in session

There are two ways to working with session, First is to Request instance and second use session() global helper method.

// Request instance
$request->session()->put('key','value');

// global session helper
session(['key' => 'value']);

Get data with specific key

To get data with specific key, use bellow methods.

// Request instance
$value = $request->session()->get('key');

// global helper method
$value = session('key');

// return session data with default value if not specific session key not found
$value = session('key', 'default');

If you would like to retrieve all data store in session, use can use all() method.

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

Check session data with key

If you want to check if specific key exist in session, you may use has() method.

if ($request->session()->has('users')) {
    // 'user' key is in session
    $user = $request->session()->get('users');
}

If you also want to check if session value is null, then use exist() method.

if ($request->session()->exists('users')) {
    // 'user' is exist in session
    $user = $request->session()->get('users');
}

Push to array session values

If you want to push value to existing session array, you can use push() method. This will set new value for frameworks key of php array.

$request->session()->push('php.frameworks', 'laravel');

Delete session data

If you want to remove specific key from the array, then use forget() method.

$request->session()->forget('key');

Or if you want to retrieve and remove the key, then use pull() method.

$value = $request->session()->pull('key', 'default');

If you want to delete multiple session data, then pass array of keys to forget() method.

$request->session()->forget(['key1', 'key2']);

If you want to delete all session data, then use flush() method.

$request->session()->flush();

You may manually regenerate the session ID, you may use the regenerate() method. This prevents users to make malicious fixation in session.

$request->session()->regenerate();

I hope you may liked this short article in your way. Thanks for supporting us.


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

No Comments