laravel how to check if session variable is empty

Viewed 17242

How can I check if this session is empty, here example

 @elseif {{Session::get('package_id')}}=null
 @include('index.customer.customerpackage')

and how to check if {{Session::get('package_id')}}=1 then Free else Paid

3 Answers

You can use

@elseif (session()->has('package_id'))

to verify if it's in session or in case it might be in session but also set to null, you can use:

@elseif (session()->get('package_id'))

Here:

$value = $request->session()->get('package_id', function () {
    return 'Return this if session key does not exist';
});

As of now Laravel 8.XX there are several ways:

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.

if (session()->has('users')) {
    //
}

To determine if an item is present in the session, even if its value is null, you may use the exists method.

if (session()->exists('users')) {
    //
}

To determine if an item is not present in the session, you may use the missing method.

if (session()->missing('users')) {
    //
}
Related