0
votes

i tried to upload more than one image in database by doing this in controller:

public function store(request $request) {

     $pictures=[];
    $input=$request->all();
    if($file=$request->file('images'))  $pictures[]=$request->file('images');
    if($file=$request->file('image1'))  $pictures[]=$request->file('image1');
    if($file=$request->file('image2'))  $pictures[]=$request->file('image2');
    if($file=$request->file('image3'))  $pictures[]=$request->file('image3');
    if($file=$request->file('image4'))  $pictures[]=$request->file('image4');
    if($file=$request->file('image5'))  $pictures[]=$request->file('image5');
    if($file=$request->file('image6'))  $pictures[]=$request->file('image6');

     foreach($pictures as $file)


    for($name=0;$name<=7;$name++)
    {
        $name=$file->getClientOriginalName();
    }
        $file->move('image',$name);

        $input['images']=$name;
        $input['image1']=$name;
        $input['image2']=$name;
        $input['image3']=$name;
        $input['image4']=$name;
        $input['image5']=$name;
        $input['image6']=$name;




  Detail::create($input);

    return redirect('/');


}

It takes images form form and stores the selected images in public/image folder but in database it stores all the images with same image name. and while displaying also it displays the same image many time.

I know guys here have solution to this and may have better idea. so please help me out in this. thanks in advance.

The above done method was not appropriate for me so i did this in my controller

     public function uploadSubmit(request $request)
{
    // Coming soon...
    $data=$request->all();
     $imagename =[];
    $i = 0;
    $files =Input::file('images');
    foreach($files as $file){
        $extension = $file->getClientOriginalExtension();
        $imagename[$i] = 'post'.str_random(10).'.jpg';
        $destinationPath =  'assets/posts';
        $file->move($destinationPath, $imagename[$i]);
        $i++;
    }
    Detail::create($files);
        return redirect('/');
    }

And in route:

    Route::resource('/details','DetailController');

Now i am getting an error like this:FatalThrowableError in DetailController.php line 43: Call to a member function getClientOriginalName() on array. Can anyone point the problem here. thanks.

1
What version of Laravel are you doing this for? Would help with providing a proper request example. You would send all of the images as an array to PHP using the same name. Are you POSTING to PHP through a standard form or javascript?Patrick Helms
I am using laravel 5.4.12.Suz Aann shrestha

1 Answers

1
votes

So I would setup a custom Request to validate the uploads are images and then you can upload them directly in your controller as shown in the examples below.

app\Http\Requests

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ImageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {

        // Logic to authorize the request
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            // Any other required laravel validations
        ];

        if (is_array($this->request->get('images'))):
            foreach ($this->request->get('images') as $key => $val):
                if($key == 0)  continue;
                $rules['images.' . $key] = 'required|image';
            endforeach;
        endif;
        return $rules;
    }
}

app\Http\Controllers\ImageController

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\ImageRequest;

class ImageController extends Controller
{
    public function store(ImageRequest $request) {  
        $uploadedImagesPath = array();
        foreach($request->input("images") as $key => $image):
            $thisFile = $request->file("images")[$key];
            if($thisFile->isValid()):

                // Get original file name 
                $imageName = $thisFile->getClientOriginalName();                

                // Upload file to default Laravel configured upload location
                $uploadedImagesPath[$key] = $thisFile->storeAs('images', $imageName); 
                // Function was in your example code
                // Detail::create($request->file("images"));

            endif;
        endforeach;
        return redirect('/');
    }

}

You can read more about the topic on Laravel's site

https://laravel.com/docs/5.4/requests#retrieving-uploaded-files

https://laravel.com/docs/5.4/validation#form-request-validation