Juluri Vinay Juluri Vinay - 3 months ago 38
PHP Question

how to save multiple images in table using laravel php

Hi please help me am new to laravel
I want to store multiple images in table... With this code am unable to save.

Help me for this..

Here in my View

{{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()}}


In my Controller

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;

Answer

It's not advisable to store images on database. Just save the path of the images instead.

If you really need to store image on db. Make sure you set the column to blob as it need more space. Then, get the image content and type, then save it.

<?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();

// ...