Laravel Session

The session is used to store small information for a limited time. You can pass session value across the different pages within same domain or website.

Laravel session

Laravel provides different drivers or types to handle the session. You can configure session using different drivers.

A driver can be file session, cookie session, array session, Memcached, Redis, APC or database driver.

You do not need to handle $_COOKIE variable manually. Laravel do it by the smart way. Laravel provides some default method to handle the session. One more thing if you want to change session driver you have to configure session.php inside config folder.

Default file driver Laravel use. Let’s take an example how you can create the session.

Laravel store your session into key and value pair. You can use “Session” class or session() method both will work. Additionally, also you can use Request class object to handle the session. Once you store session you can get it using get() method.

You can remove session by using forget method.

To remove all session you can use flush() method

To get all value of session you can use all() method.

You can store anything in a session that can be a string, number of the even array also. Here take an example:

If you are using file session You can take note that Laravel store unique file into storage/framework/sessions directory.

A directory contains the number of file with a unique name. Your information stored into serialize format into a file.

You can check that session is exists or not by using has() method. You can also get a session on blade file.

Here I checked if a session exists then a print value of it.

Laravel also provide session flash. Flash session is auto expire once page refresh. Flash means single time use of session.

You can set session flash by using flash() method in Laravel 5.4.

To access it on view page same you can use session get() method.

Leave a Reply

Your email address will not be published. Required fields are marked *