Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if session key exists in Laravel 5.1?

I'm trying to check it a session key has already been set inside a controller. The documentation states that it is possible to check if an item exists in an array and that's it.

http://laravel.com/docs/5.1/session

like image 231
AlGallaf Avatar asked Jun 29 '15 08:06

AlGallaf


People also ask

How do you check session is set or not in laravel blade?

You can check whether a variable has been set in a user's session using the function isset(), as you would a normal variable. Because the $_SESSION superglobal is only initialised once session_start() has been called, you need to call session_start() before using isset() on a session variable.

How check session is null in laravel?

To determine if an item is present in the session, you may use the has method. The has method returns true if the item is present and is not null. To determine if an item is present in the session, even if its value is null, you may use the exists method.

How does laravel session work?

Sessions are used to store information about the user across the requests. Laravel provides various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used because it is lightweight. Session can be configured in the file stored at config/session.


2 Answers

you can use

if($request->session()->has('key'))
{
}
like image 123
Tariqul Islam Avatar answered Sep 18 '22 13:09

Tariqul Islam


Has pointed out by @DavidDomain probably the best way of doing it is to use

if(Session::has('...'))

Worked like a charm for me.

like image 44
Baldráni Avatar answered Sep 18 '22 13:09

Baldráni