1 Answer 3397 Views
Let's create a function to upload image first.
public function uploadimage($foldername, $imageindex){ $name = $imageindex->getClientOriginalName(); $imageindex->move(public_path().'/'.$foldername,date('ymdgis').$name); return date('ymdgis').$name; }
$foldername is the directory where the uploaded images will be stored and imageindexis the array where the image informations will be stored.
move() function will move the file from temporary location to the folder location we define and the image name will be concatenated at the end of datetime and returned.
The only thing we need to do now to process the image is call this function in our method to insert image. Let's do that then.
public function addToGallery(){ $data = Input::except('_token'); $imgs = $data['uploads']; foreach($imgs as $img){ $data['uploads'] = $this->uploadimage($foldername, $img); DB::table($tbl)->insert($data); } }
$data will store all the values sent from the form as array excluding the csrf_token which is required only to send the form data.
Since the uploads fieldname contains another array of image inside we stored it in a variable and iterated the values using forloop.
Finally, we'll call the upload function inside the loop to store image in directory and it's name in database while inserting the images in individual row in database while remaining inside the loop.
If you want to add all the images in a single column of a single row, you can use expolde function before uploading function and then use implode again before inserting into the table.
Don't forget to define field name as an array in the form file.
<input type="file" name="uploads[]" select="multiple">
Leave a comment