我试图保存多个图像和值的数组,这些值保存得很好但是当我要添加图像时,它只保存一个图像并只上传一个。
这是我的控制器功能
public function store(Request $request) {
$parentproduct = new Product();
$parentproduct->id = Input::get('id');
$parentproduct->save();
$insertedId = $parentproduct->id;
$uploadcount=0;
$files = Input::file('main_image');
$file_count = count($files);
foreach($files as $i=>$file) {
$multiupload=new ProductsTranslation();
if($request->hasFile('main_image')){
$destinationPath = 'website/images';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
$uploadcount ++;
$multiupload->main_image = $filename;
$multiupload->id = $request->input('id')[$i];
$multiupload->title = $request->input('title')[$i];
$multiupload->language = $request->input('language')[$i];
$multiupload->product_id=$parentproduct->id;
$multiupload->save();
}
}
最终更新后它正常工作......
答案 0 :(得分:0)
试试这个:
if (Input::hasFile('main_image')) {
foreach (Input::file('main_image') as $file) {
$destinationPath = 'website/images';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
$uploadcount ++;
// You have to initialize your array out side your loop
$insertprod = [];
foreach ($request->input('language') as $i=>$language) {
$insertprod[] = array(
'id' =>$request->input('id')[$i],
'product_id'=>$parentproduct->id,
'title' =>$request->input('title')[$i],
'language' => $request->input('language')[$i],
//used this line to save the image name path !
'main_image'=>$filename[$i]
);
}
}
DB::table('products_translations')->insert($insertprod);
}