我有一個laravel項目,它是分類網站,每個廣告都應該附上幾張照片。我製作了兩個模型,其中包含兩個表格,一個用於廣告,另一個用於圖片,關係正確設置媒體表格具有外鍵advert_id Dropzonejs用於表單中,但URL指向MediaController中的函數廣告的添加由AdvertController管理現在,問題出現在這兩個單獨的工作場所,當我上傳圖片時我不知道advert_id的價值,但我無法想象如何讓用戶先保存廣告,然後上傳圖片。我不想在兩個頁面上分割表單,但不知道如何操作用戶來完成我所需要的操作。 這裏是我的管理懸浮窗代碼: $('.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') }}", });
Dropzone + Laravel在一個地方形成
這裏是管理上傳中的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)並在創建廣告後引用此會話變量來解決此問題。將媒體鏈接添加到數據庫表後,刷新會話。 仍認爲這不是應該如何做,而是作爲臨時解決方案。 – brack11
仍然不可靠的解決方案,仍在尋求幫助 – brack11