15

I am trying to upload multiple images in a single row in a database table and access them in a show page.I have tried this tutorial: laraveldaily.com/upload-multiple-files-laravel-5-4/ but there two different tables are made and a relation is established.

I want this to happen in a single table.

3
  • 3
    Show your code what you have tried so far Commented Mar 7, 2017 at 8:14
  • I am following tutorial : laraveldaily.com/upload-multiple-files-laravel-5-4 Commented Mar 7, 2017 at 8:15
  • 2
    Ok. Have you tried something? Commented Mar 7, 2017 at 8:16

6 Answers 6

43

here is what worked best for me:

first do this in your form:

<form class="form-horizontal" enctype="multipart/form-data" method="post" action="/details">

and this for multiple selection:

<input required type="file" class="form-control" name="images[]" placeholder="address" multiple>

Now do this in your controller:

public function store(request $request) {

    $input=$request->all();
    $images=array();
    if($files=$request->file('images')){
        foreach($files as $file){
            $name=$file->getClientOriginalName();
            $file->move('image',$name);
            $images[]=$name;
        }
    }
    /*Insert your data*/

    Detail::insert( [
        'images'=>  implode("|",$images),
        'description' =>$input['description'],
        //you can put other insertion here
    ]);


    return redirect('redirecting page');
}

Hope this works

Sign up to request clarification or add additional context in comments.

3 Comments

hi i'm wondering how about if i use validation, what should i write? $data = $request->validate(['name'=>'required', 'files' => 'file']); it kinda work but i'm unable to fetch the file data after that
something like : $this->validate($request, [ 'files' => 'array|required', 'files.*' => 'required|mimetypes:image/jpg,image/jpeg,image/bmp' ]);
Working Awesome Brother
3
<form role="form" method="post" action="{{URL::to('addimage')}}" enctype="multipart/form-data">
    <div class="form-group" style="padding-bottom: 15px">                            
         <label class="col-lg-3">Upload</label>
         <input class="btn btn-primary"  type="file" name="files[]" > <br/>
    </div>
</form>




$images = $request->file('files');
if ($request->hasFile('files')) :
        foreach ($images as $item):
            $var = date_create();
            $time = date_format($var, 'YmdHis');
            $imageName = $time . '-' . $item->getClientOriginalName();
            $item->move(base_path() . '/uploads/file/', $imageName);
            $arr[] = $imageName;
        endforeach;
        $image = implode(",", $arr);
else:
        $image = '';
endif;



        DB::table('fooo')->insert(
        array(
            'image' => $image
           )
       );

        Session::flash('message', 'Image upload successfully successfully');
        return redirect('/addimage');

Comments

2

this can also be achieved with

            foreach ($request->file('files') as $index => $item) {
                $path = $request->files[$index]->store('images');
            }

Comments

2

This one works for me.

Form:

<form class="form-horizontal" enctype="multipart/form-data" method="post" action="/details">

and this for multiple selection image:

<input required type="file" class="form-control" name="image[]" placeholder="address" multiple>

Now do this in Our Model:

protected fillable = ['producd','image', ];

Now do this in Our controller:

public function store(Request $request)
{
  $image = array();
  if($file = $request->file('image')){
      foreach($file as $file){
          $image_name = md5(rand(1000,10000));
          $ext = strtolower($file->getClientOriginalExtension());
          $image_full_name = $image_name.'.'.$ext;
          $uploade_path = 'uploads/images/';
          $image_url = $uploade_path.$image_full_name;
          $file->move($uploade_path,$image_full_name);
          $image[] = $image_url;
      }
      Image::insert([
          'image' => implode('|', $image),
          'producd' => $request->producd,
      ]);
      return redirect('/image.index'));       
  }
}

2 Comments

how to fectch and displpay the Multiple images in index page in laravel
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1

html

<input type="file" name="images[]" multiple>

controller : store folder storage

foreach ($request->file('images') as $imagefile) {
   return $imagefile->store('images','public');
}

Comments

0
    $images = array();
    if ($request->hasFile('gallery_image')) {
        $files = $request->file('gallery_image');
        foreach ($files as $file) {
            $image_name = md5(rand(1000, 10000));
            $ext = strtolower($file->getClientOriginalExtension());
            $image_full_name = $image_name . '.' . $ext;
            $file->move('Your image path here', 
            $image_full_name);
            $images[] = $image_full_name;
        }
    }
    $house->gallery_image = json_encode($images);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.