使用Eloquent在One to Many关系中插入数据时遇到了一些麻烦。当我将数组传递给对象时,文档声明会抛出错误,当我回显$ex->getMessage()
时,只会打印我想要传递的数组的索引(“数字”)。有没有人知道为什么会这样?
public static function insert(Request $request, Response $response) {
try {
$data = $request->getParsedBody();
$user = new \User;
$user->name = $data['name'];
$user->flag = $data['flag'];
$user->login = $data['login'];
$user->password = md5(SALT . $data['password']);
//$user->save();
foreach ($data['phoneNumbers'] as $key => $number) {
$phonenumber[$key] = new \Phonenumber(array("number" => $number));
}
$user->phonenumbers()->saveMany($phonenumber);
$meta = Helper::retornaMetaArray(Enum::SUCS_STS, Enum::CREATED, 201);
return $response->withCustomJson(null, $meta);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
这是我在请求正文中发送的内容:
{
"name": "Jon Snow",
"login": "jon",
"password": "ghost",
"flag": "1",
"phoneNumbers": ["(82) 99178-1066", "(82) 99303-9037"]
}