Laravel Store Array in Session(Laravel 在会话中存储数组)
问题描述
我在会话中存储数组时遇到问题.我正在制作购物车,但它似乎不起作用.
I have been having trouble storing an array in session. I am making a shopping cart and it doesn't seem to work.
public function __construct(){
$product = array(1,2,3,4);
Session::push('cart', $product);
}
然后像这样在视图中检索它.
and then retrieve it in the view like this.
{{Session::get('cart')}}
但是我一直收到这样的错误.
However I keep getting an error like this.
htmlentities() expects parameter 1 to be string, array given
有关如何创建可存储一系列商品的购物车的任何线索和建议.
Any clues and advice on how to create a shopping cart that stores an array of items.
推荐答案
如果你需要使用 session 中的数组作为字符串,你需要像这样使用 Collection:
If you need to use the array from session as a string, you need to use Collection like this:
$product = collect([1,2,3,4]);
Session::push('cart', $product);
当您在 html 中使用 {{Session::get('cart');}} 时,这将使它起作用.请注意 Session::push 因为它会在会话中始终附加新产品.您应该使用 Session::put 以确保产品将始终更新.
This will make it work when you will be using {{Session::get('cart');}} in your htmls. Be aware of Session::push because it will append always the new products in sessions. You should be using Session::put to be sure the products will be always updating.
这篇关于Laravel 在会话中存储数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 在会话中存储数组
基础教程推荐
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
