13

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 Mar 7, 2017 at 8:14
  • I am following tutorial : laraveldaily.com/upload-multiple-files-laravel-5-4
    – Rojina
    Mar 7, 2017 at 8:15
  • 2
    Ok. Have you tried something? Mar 7, 2017 at 8:16

6 Answers 6

41

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

3
  • 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 Feb 11, 2020 at 16:16
  • 1
    something like : $this->validate($request, [ 'files' => 'array|required', 'files.*' => 'required|mimetypes:image/jpg,image/jpeg,image/bmp' ]);
    – mercury
    Feb 15, 2022 at 21:12
  • 1
    Working Awesome Brother
    – Keyur Vala
    Aug 7, 2022 at 6:27
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');
2

this can also be achieved with

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

html

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

controller : store folder storage

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

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
  • how to fectch and displpay the Multiple images in index page in laravel Sep 21, 2021 at 11:33
  • 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.
    – Community Bot
    Sep 21, 2021 at 11:43
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
  • 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.
    – Community Bot
    Oct 21, 2022 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.