Dropzone + Laravel形成于一个地方

时间:2014-07-11 17:11:49

标签: forms laravel upload eloquent dropzone.js

我有一个laravel项目,这是分类网站,每个广告都应附上一些照片。我在db 1中制作了两个带有两个表的模型用于广告另一个用于pics,关系设置正确媒体表有外键ads_id Dropzonejs用于表单但是它指向来自MediaController的函数的URL广告的添加由AdvertController管理现在当我上传图片我不知道advert_id值时,问题出现在这两个独立的工作场所,但我无法想象如何让用户先保存广告,然后再上传图片。我不想在两页上分割表格,但不知道如何操纵用户做我需要的事情。 这是我管理dropzone的代码: $('.dropzone-stuff').dropzone({ url: "{{ URL::to('upl') }}", paramName: 'file', autoProcessQueue: true, maxFiles: 5, addRemoveLinks: true, clickable: true, dictDefaultMessage: "{{ trans('custom.drop.dictDefaultMessage') }}", dictFallbackMessage: "{{ trans('custom.drop.dictFallbackMessage') }}", dictFallbackText: "{{ trans('custom.drop.dictFallbackText') }}", dictInvalidFileType: "{{ trans('custom.drop.dictInvalidFileType') }}", dictFileTooBig: "{{ trans('custom.drop.dictFileTooBig') }}", dictCancelUpload: "{{ trans('custom.drop.dictCancelUpload') }}", dictRemoveFile: "{{ trans('custom.drop.dictRemoveFile') }}", dictMaxFilesExceeded: "{{ trans('custom.drop.dictMaxFilesExceeded') }}", });

以下是在MediaController中管理上传的部分:

public function uploadImages()
    {
        if (Sentry::check()) {
            $file = Input::file('file');
            $advert_id = Advert::orderby('id', 'desc')->first()->id;
            $destinationPath = public_path().'/uploads/'.Sentry::getUser()->username.'/';
            $filename = str_random(6);
            $extension =$file->getClientOriginalExtension();
            $filename = $filename.'.'.$extension;
            $upload_success = Input::file('file')->move($destinationPath, $filename);
            if( $upload_success ) {
                return Response::json('success', 200);
            } else {
                return Response::json('error', 400);
            }
        }
    }

好吧,我认为在这里发布所有内容的代码太多了,但我只是告诉需要 - 我必须在上传之前使用广告ID将其用于媒体表中的advert_id列。

1 个答案:

答案 0 :(得分:0)

我处理类似情况的方法是首先创建实体(广告),然后为您计划添加到其中的媒体设置第二页或步骤。由于该实体存在 - 并且可以发布到您上传媒体的下一页,您只需将其添加到您的脚本

myDropzone.on("sending", function(file, xhr, formData) {
   formData.append('advert_id', <?php echo $advert_id; ?>);
});

然后在 upl 文件中,您可以将其关联。

相关问题