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.
8 Answers
you can use
if($request->session()->has('key'))
{
}
-
1
-
in laravel 5 or its upper syntax has been changed that's why you will write code like this for check the session is exist or not. read the session documentation for laravel 5.2. it makes you concept clear. Jul 11, 2016 at 16:50
Has pointed out by @DavidDomain probably the best way of doing it is to use
if(Session::has('...'))
Worked like a charm for me.
you can use Session::has('YOUR_SESSION_KEY')
in both blade and controller
controller ex:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
class add_classController extends Controller
{
public function index(){
if (Session::has('YOUR_SESSION_KEY')){
// do some thing if the key is exist
}else{
//the key does not exist in the session
}
}
}
blade ex:
@if (Session::has('YOUR_SESSION_KEY'))
{{-- do something with session key --}}
@else
{{-- session key dosen't exist --}}
@endif
if($request->session()->exists('your_key'){
//
}
-
Although this code snippet may answer the question, including an explanation of why and how it helps solve the problem improves the quality and longevity of your answer. See How do I write a good answer? Jul 13, 2019 at 6:46
This works for me
@if (session()->has('hasCoupon'))
[{{ Session::get('hasCoupon')['name'] }}]</span>
@endif
@if ( session::has('message')
{{ session::get('message') }}
@endif
or
@if (session->has->('message'))
{{session->
@endif
-
Welcome to SO! Since this exact answer has already been given and the question is 6 years old, duplicate answer do not contribute a lot. Perhaps you can take a look at newer questions without answers to provide your help? Jul 9, 2021 at 15:01
if (Session::has('your_key'))
?