php - 使用嵌套的 JSON 对象循环 JSON 数组
问题描述
如何遍历 json 有效负载对象并将嵌套user_info
和address
json 对象正确分配给users
表user_info
和address
列?email
并且password
被正确分配,但是因为user_info
jsonaddress
对象它不起作用
用户表
email (string),
password (string),
user_info (json),
address (json)
JSON有效负载
{
email: 'testing@gmail.com',
password: 'password',
user_info: {
first_name: 'testing'
}
address: {
zip_code: 12345
}
}
控制器
protected function createOrUpdate(Request $request) {
$user = new User();
$uCols = [
'email',
'password',
'user_info.first_name',
'address.zip_code',
];
$input = $request->only($uCols); // input is the json payload above
foreach($uCols as $column) {
if(array_key_exists($column, $input) && !empty($input[$column]))
{
$user->$column = $input[$column]; //email and password are correctly assigned
}
}
$user->save();
}
解决方案
我想到了。我必须更新我的可填充列数组以仅包含列名而不包括属性user_info.first_name
$uCols = [
'email',
'password',
'user_info',
'address',
];
推荐阅读
- typescript - 如何在 Async/await 中转换函数?
- python - 为什么 `tf.pad` 填充参数需要额外的增量以提高准确性?
- laravel - laravel 5.7 whereNull
- python - IndexError:弹出索引超出范围
- php - 即使声明了未定义的索引
- unity3d - 如何在单击按钮之前暂停所有脚本?
- c# - 如何截取最顶部窗口后面的窗口的屏幕截图?
- c++ - std::async lambda 函数中局部变量的生命周期
- angular - Angular Material Slider - 如何始终显示带有值和自定义的刻度间隔?
- google-cloud-platform - 为什么 GCSfuse 看不到 GS 存储桶,而 GSutil 可以?