简体   繁体   中英

Multiple image upload in database and showing them in Laravel

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM