以下编码示例不起作用。
$joiner = new Entity();
$joiner->name = $post['name'];
$joiner->address = $post['address'];
$meeting = Meeting::first($_id);
$meeting->joiner[] = $joiner;
$meeting->save();
我希望每次表单发布的时候都能成功。应将新的加入者实体推入会议对象的“加入者”属性中。
如何纠正它?并感谢所有帮助。
更新
现在我已经像这样改变了它。它工作正常,但仍然不是很好。
$joiner = new Entity();
$joiner->name = $post['name'];
$joiner->address = $post['address'];
$meeting = Meeting::first($_id);
if(empty($meeting->joiner)){
$joiners = array($joiner);
}
else{
$joiners = array();
foreach($meeting->joiner as $one){
$joiners[] = $one;
}
$joiners[] = $joiner;
}
$meeting->joiner = $joiners;
$meeting->save();