Laravel - 如何在视图中使用/访问会话变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48097269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - How to use/access session variables in a view?
提问by Black
I only find information on how to save a session variable which works like this: $request->session()->put('key', 'value');
我只找到有关如何保存会话变量的信息,其工作方式如下: $request->session()->put('key', 'value');
But how can I access the session in a blade view?
但是如何在刀片视图中访问会话?
回答by Alexey Mezenin
Just use session()
helper to read the data:
只需使用session()
helper 读取数据:
{{ session('key') }}
Or:
或者:
{{ request()->session()->get('key') }}
If you want to store something, do this:
如果您想存储一些东西,请执行以下操作:
@php(session(['key' => 'value']))
回答by Rits
Its quite simple as core php, you can use its facade in blade or a method like,
它作为核心 php 非常简单,您可以在刀片中使用它的外观或类似的方法,
{{Session::get('key')}}
Or by method like,
或者通过类似的方法,
{{ session()->get('key') }}
回答by Rodolfo Velasco
In case you have a session variable called $mySessionVar and inside you have key value pair information then you could use:
如果您有一个名为 $mySessionVar 的会话变量,并且内部有键值对信息,那么您可以使用:
<p>
@if(Session::has('$mySessionVar'))
{{ Session::get('mySessionVar')['keyName'] }}
@endif
</p>
And in case the value is an array (key/value pair) you could use:
如果值是一个数组(键/值对),您可以使用:
@foreach(Session::get('mySessionVar')['keyName'] as $variableName)
<span>{{ $variableName }} </span>
@endforeach