3

I am trying to Upload multiple images into a database but only one is uploading instead of multiple.

How to upload multiple images into a database?

Could anyone tell me what is wrong with my code?

        [database table ][1]
        [1]: https://i.sstatic.net/kgv4r.png  

controller

public function singalprojectaction(Request $request)
  {
   $input=$request->all();
   $images=array();
   if($files=$request->file('images')){
   foreach($files as $file){
   $name=$file->getClientOriginalName();
   $file->move(public_path('projects'), $name);
   $images[]=$name;
   }
   }
   $query=DB::table('single_portfolio')->insert( [
  'Project_name' =>$input['project_name'],
  'Client_Name' =>$input['Client_name'],
  'Completion_date' =>$input['Completion_date'],
  'Duration' =>$input['Duration'],
  'project_image_one'=>  implode("|",$images),
  'Description' =>$input['Description'],
   'project_id' =>$input['select_project'],
   ]);
  if($query)
   {
   return response()->json([
  'message'   => 'Image is Successfully Inserted',
  'class_name'  => 'alert-success'
  ]);
   }
  else{

 return response()->json([
'message'   => 'Data  is not inserted Inserted',
'class_name'  => 'alert-warning'
 ]);

  }
  }

html view

<form action="Route('singal.action') }}" id="singal_project" 
   enctype="multipart/form-data">
   {{ csrf_field() }}
   <div class="alert" id="message" style="display:block;"></div>
   <div class="group-form">
      <label>Drop Multple Imges</label>
      <input required type="file" class="form-control" name="images[]"
         multiple>
   </div>
</form>
4
  • You mean you are getting single image in $images ? Commented Dec 24, 2019 at 8:51
  • yes you are saying right i am uploading multiple images but just first one image is uploading into database Commented Dec 24, 2019 at 8:55
  • can you share you html file tag also ? Commented Dec 24, 2019 at 8:56
  • please check html code i have uploaded Commented Dec 24, 2019 at 9:11

2 Answers 2

2

Try This To insert multiple images

 public function singalprojectaction(Request $request)
  {
     $input=$request->all();
     $datas = [];
     $result = [];
     if ($request->hasfile('images')) {
        foreach ($request->file('images') as $key => $file) {
        $name = $file->getClientOriginalName();
        $file->move(public_path() . '/projects/', $name);
             $datas[$key] = $name;
        }
    }

        $query=DB::table('single_portfolio')->insert( [
                  'Project_name' =>$input['project_name'],
                  'Client_Name' =>$input['Client_name'],
                  'Completion_date' =>$input['Completion_date'],
                  'Duration' =>$input['Duration'],
                  'project_image_one'=>  implode("|",$datas);
                  'Description' =>$input['Description'],
                  'project_id' =>$input['select_project'],
                   ]);
        if($query){
          return response()->json(['message'   => 'Image is Successfully Inserted','class_name'  => 'alert-success']);
                   }
        else{
          return response()->json(['message'   => 'Data  is not inserted Inserted','class_name'  => 'alert-warning'
                 ]);
           }
        }
Sign up to request clarification or add additional context in comments.

2 Comments

i have used this code but still first one image is uploading into database
0

You will have to wrapped the insert code in the foreach statement: see below

foreach($files as $file){
                   $name=$file->getClientOriginalName();
                   $file->move(public_path('projects'), $name);


 $query=DB::table('single_portfolio')->insert( [
                      'Project_name' =>$input['project_name'],
                      'Client_Name' =>$input['Client_name'],
                      'Completion_date' =>$input['Completion_date'],
                      'Duration' =>$input['Duration'],
                      'project_image_one'=>  $name),
                      'Description' =>$input['Description'],
                       'project_id' =>$input['select_project'],
                       ]);

                   }

Comments

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.