【发布时间】:2014-09-02 09:27:50
【问题描述】:
我有一个 laravel 项目,它是分类网站,每个广告都应该附有几张照片。我已经制作了两个模型,在 db 中有两个表,一个用于广告,另一个用于图片,关系设置正确 媒体表具有外键 advert_id Dropzonejs 在表单中使用,但它的 URL 指向来自 MediaController 的函数 广告的添加由 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 列。
【问题讨论】:
-
通过添加 Session::push('files',$filename) 然后在创建广告后引用此会话变量来解决它。将媒体链接添加到数据库表后,刷新会话。仍然认为这不是应该这样做的,但作为一个临时解决方案很好。
-
仍然不可靠的解决方案,仍在寻求帮助
标签: forms laravel upload eloquent dropzone.js