我有一个帖子和附件模型。帖子模型有标题,身体属性。附件具有标题,文件属性。发布has_many附件。
在单一表单中,我希望用户能够添加/编辑多个附件。
现在我这样做:
控制器@编辑:
表单视图:
attachments[id][title]
Math.random()
)控制器@更新:
Input::get('attachments')
withInput()
和withErrors()
但是,返回时,错误与输入字段没有正确关联。我认为这是因为我使用数组而不是对象。有没有更好的方法呢?
答案 0 :(得分:1)
关于laravel,我可能会先创建附件对象,然后将对象添加到帖子中。 Inserting Related Models
下的文档中也对此进行了解释$atachments = array(
new Attachment(array('title' => 'Attachment #1.')),
new Attachment(array('title' => 'Image Lol.')),
new Attachment(array('title' => 'The answer is 42.'))
);
$post = Post::find(1);
$post->attachments()->saveMany($comments);
您应该能够以相同的方式更新您的评论(当然,不需要实例化新的附件对象)。