保存输入文件的数据多次

时间:2015-09-30 10:06:26

标签: laravel octobercms

我正在尝试保存帖子中的文件列表,但它不会保存关系。

我想保存第一篇文章,然后添加插入的图片。

class SaveData extends ComponentBase {
    public function onSubmitContact() {
        /*
         * This field: <input type="file" name="files[]" id='files' multiple="true"/>
         */
        $files = Input::file('files');

        $modelFiles = new MyModeToSave;
        $modelRelation = new ModelToRelation;

        foreach($files as $file):
            $modelFiles->data = $file;
            $modelFiles->save();
        endforeach;

        $modelRelation->title = post('title');
        /* more fields */
        $modelRelation->save();

        $modelRelation->files()->add($modelFiles);
    }
}

罐 代码是CMS OCTOBER

1 个答案:

答案 0 :(得分:0)

不确定您的确切用例,但这样的事情应该有效:

    //Pass your model id, and then find it in your function
    $model = ModelToRelation::find(post('modelID'));
    //commit all the files relations
    $model->commitDeferred(post('_session_key'));

    foreach($model->files() as $file) {
        //now you can do stuff with the file...or model
    }