我正在尝试检查控制器中是否已经设置了会话密钥。文档表明,可以检查数组中是否存在项目,仅此而已。
你可以使用
if($request->session()->has('key'))
{
}
正如在问题评论中@DavidDomain所指出的那样,最好的做法可能是使用
if(Session::has('...'))
您可以在Blade模板和控制器中使用Session::has('YOUR_SESSION_KEY')
。
例如在控制器中:
<?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示例:
@if (Session::has('YOUR_SESSION_KEY'))
{{-- do something with session key --}}
@else
{{-- session key dosen't exist --}}
@endif
if(Session::has('your_key')){
return $next($request);
}
这对我来说可行
@if (session()->has('hasCoupon'))
[{{ Session::get('hasCoupon')['name'] }}]</span>
@endif
if($request->session()->exists('your_key'){
//
}
if(session()->exists('key_here'))
尝试使用这个新的 Laravel 语法
@if(session()->has('success'))
//rest of code here
@endif
@if ( session::has('message')
{{ session::get('message') }}
@endif
或者
@if (session->has->('message'))
{{session->
@endif
if (Session::has('your_key'))
? - DavidDomain