我在Laravel的数据库中使用不同的ID一次保存多个图像

时间:2017-04-22 13:12:12

标签: laravel laravel-5 laravel-5.2

  

并保存,但问题是所有图片都有不同的自动   数据库中的增量ID我想用相同的ID保存图像   同时保存,请指导我。

****强文**代码如下; **

$field = Input::file('image');

$location = public_path('uploads/');
        foreach ($field as $k => $fd) {
            $filename = str_random(2).'-' . time() . "." . $fd->getClientOriginalExtension();
            $fd->move($location , $filename);
            $product->image = $filename;
        }
        $user->save();

1 个答案:

答案 0 :(得分:0)

    I think bellow code helps you..
You can try.

$picture = '';

if ($request->hasFile('image')) {

    $files = $request->file('image');
    foreach($files as $file){
        $filename = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();
        $picture = date('His').$filename;
        $destinationPath = base_path() . '\public\images';
        $file->move($destinationPath, $picture);
    }
}

if (!empty($product['images'])) {
    $product['images'] = $picture;
} else {
    unset($product['images']);
}