在 Blade 中使用会话数据 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41444790/
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
Use session data in Blade - Laravel
提问by WellNo
I've created a new session in my controller and filled it with some data from my database.
我在我的控制器中创建了一个新会话,并用我的数据库中的一些数据填充了它。
Thats the controller code:
那是控制器代码:
public function index(Request $request) {
$products = Products::with('categories')->get()->take(5);
$request->session()->put('session_products', $products);
return view('shoppingcard.card');
}
and in the next controller function I retrieve the session data.
在下一个控制器函数中,我检索会话数据。
public function receiver(Request $request) {
if($request->session()->has('session_products')) {
echo $request->session()->get('session_products');
}
else return "stop no session";
return view('shoppingcard.endview');
}
Now if I do a : return $request->session()->get('session_products');
I get everything I need.
现在如果我做一个:return $request->session()->get('session_products');
我得到了我需要的一切。
Here is a dummy data of the content from the session:
这是会话内容的虚拟数据:
{"id":5,"title":"Beautiful Unicorn","price":"20.00","sale_price":"15.00","status":"active","created_at":null,"updated_at":null,}
Now I'm in the endview blade and want to output the Data.
现在我在 endview 刀片中并想输出数据。
How can I output specific Data like the "title" ?
我怎样才能输出像“标题”这样的特定数据?
采纳答案by Antonio Carlos Ribeiro
That data is serialized to session and unserialized back to a object. All you need is to use that info exactly the way you normally would:
该数据被序列化到会话并反序列化回对象。您所需要做的就是完全按照通常的方式使用该信息:
public function receiver(Request $request) {
if(! $request->session()->has('session_products')) {
return "stop no session";
}
return view('shoppingcard.endview')
->with('title', $request->session()->get('session_products')[0]['title']);
}
Or you can just pass it all to your view:
或者您可以将其全部传递给您的视图:
public function receiver(Request $request) {
if(! $request->session()->has('session_products')) {
return "stop no session";
}
return view('shoppingcard.endview')
->with('products', $request->session()->get('session_products'));
}
And use it in the view:
并在视图中使用它:
@foreach($products as $product)
{{ $product['title'] }}
@endforeach
回答by Kris Roofe
your session save the products as json. you can decode it with json_decode, then access each of the products.
您的会话将产品保存为 json。您可以使用 json_decode 对其进行解码,然后访问每个产品。
code as:
代码为:
$products = json_decode($request->session()->get('session_products'), true);
foreach($products as $product)
{
echo $product['title'];
}
回答by Jesse de gans
Well in order to access the title element you need to convert the json string to a array. To do this you need to use php's json_decode
function to decode the string and then access the array like so:
好吧,为了访问标题元素,您需要将 json 字符串转换为数组。为此,您需要使用 php 的json_decode
函数来解码字符串,然后像这样访问数组:
$decodedJsonString = json_decode($request->session()->get('session_products'), true);
access it like $decodedJsonString[id]['title'];
访问它就像 $decodedJsonString[id]['title'];
UPDATE #1
更新 #1
use laravel's with() or compact() method to send it to the view
使用 Laravel 的 with() 或 compact() 方法将其发送到视图
with() and compact are both eloquent methods
with() 和 compact 都是雄辩的方法