如何解决“ message”:“非法字符串偏移量'product_id'”,在Laravel RESTfull API中
Am正在开发Laravel RESTful API,使用PHP关联数组将Json对象的数组保存到DB中,这是错误的体现。
"message": "Illegal string offset 'product_id'","exception": "ErrorException",...
这是要保存在MysqL DB中的JSON对象数组
"userId": "1","userName": "Dan","userEmail": "dan@gmail.com"
"product": [
{
"product_id": "31","product_name": "Orange","product_price": "USD 0.5"
}
]
}
$userId = $request -> input('userId');
$userName = $request ->input('userName');
$userEmail = $request ->input('userEmail');
$data = array(
"userId"=>$userId,"userName"=>$userName,"userEmail"=>$userEmail
);
$user = User::create($data);
if($order){
//Storing the received JSON in $json.
$json = file_get_contents('PHP://input');
// Decode the received JSON and store into $obj
$obj = json_decode($json,true);
foreach($obj as $product){
$product_id = $product['product_id'];
$product_name = $product['product_name'];
$product_price = $product['product_price'];
$product = array(
"product_id" => $product_id,"product_name" => $product_name,"product_price" => $product_price
);
DB::table('products')->insert($product);
}
if($product){
// On query success it will print below message.
$MSG = 'Data Successfully Submitted.' ;
// Converting the message into JSON format.
$json = json_encode($MSG);
// Echo the message.
echo $json ;
}
else{
echo 'Try Again';
}
我在哪里弄错。预先谢谢你!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。