如何使用laravel-php在表中保存多个图像


how to save multiple images in table using laravel php

嗨,请帮我认识laravel我想在表中存储多个图像。。。使用此代码我无法保存。

帮我这个。。

在我的视图中

{{Form::open(array('url'=>'businessdirectory/business', 'files'=>true))}}
{{Form::label('image','Upload Image')}}
<div class="form-group">{{Form::file('image[]',array('multiple'=>true))}} 
</div>
{{Form::close()}}

在我的控制器中

 if(Input::file('image'))
        {
        $image = Input::file('image');
        foreach($image as $img) {
        $destination = 'images';
        $filename = $img->getClientOriginalName();
        $path = 'images/'.$filename;
        $uploadSuccess = $img->move($destination,$filename);
        }
        }
        else
        {
            $path='images/default.JPG';
        }
 $business = new Business();    
 $business->image = $path;

不建议在数据库中存储图像。只需保存图像的路径即可。

如果您真的需要将图像存储在数据库中。请确保将列设置为blob,因为它需要更多空间。然后,获取图像内容和类型,然后保存。

<?php
$image = fopen($image_path, 'rb');
// or
$image = file_get_contents($image_path);
$business = new Business;
$business->image = $image;
$business->imageType = "image/gif"; // 
$business->save();
// ...

您需要将以下代码放入foreach循环中。在每个循环中,您都需要在数据库中插入图像路径

 $business = new Business();    
 $business->image = $path;

这是laravel 5.2中的一个工作代码。

$files = $request->file('file');
 if($request->hasfile('file')){ 
    $destinationPath = 'uploads';
    foreach($files as $file){
        $image = new Image;
        $filename = $file->getClientOriginalName();
        $image->name = $filename;
        $image->save(); 
        $file->move($destinationPath,$filename);
    }