laravel - Laravel 访问控制器中的 JSON 对象
问题描述
在提交的表单保存在变量中后,我在控制器中有这个数组$product
。
[
{
"id": 2,
"name": "nancy",
"cost": 34,
"quantity": 0,
"barcode": 12345,
"category_id": 2,
"created_at": "2020-07-05T16:04:10.000000Z",
"updated_at": "2020-07-09T04:06:09.000000Z"
},
{
"id": 5,
"name": "jk",
"cost": 100,
"quantity": 2,
"barcode": 147258,
"category_id": 2,
"created_at": "2020-07-08T20:34:56.000000Z",
"updated_at": "2020-10-18T13:09:16.000000Z"
}
]
如何访问对象中的属性,如id
, name
, barcode
?
解决方案
只需解码它,使用json_decode
$products = json_decode($products, true); // When TRUE, JSON objects will be returned as associative arrays
foreach($products as $product){
echo $product['id'];
}
推荐阅读
- postgresql - 如何在 postgresql 中的 JSONB 列上添加 SQLAlchemy BTREE 索引
- python - 使用 Python OpenCV 测量已知圆的精确位置
- docker - 通过 TLS 连接到 OPENLDAP:找不到证书
- java - 在方法内更改应该在方法外的字符串
- linux - pci 驱动程序函数是否表示为 PCI 的 driver_data 对象中的函数号
- automation - 使用 QuerySerge 在 ALM 测试用例步骤级别附加屏幕截图
- reactjs - 我想在 React Native 中将 My Screen 修改为 Drawer
- security - 这些包含指向我的节点服务器的随机 URL 的 POST 请求是什么
- c# - 游戏有时无法识别触摸
- jquery - 展开并滚动到隐藏元素