1

I want to insert multiple image in this code , please help me how to insert multiple images in this code..

BlogController.php

 public function storeBlog(Request $request)
    { 
         $blogs = new Blog($request->input()) ;

         $blogs->title = $request->title;
         $blogs->blog = $request->blog;

          if($file = $request->hasFile('image')) {

            $file = $request->file('image') ;

            $fileName = $file->getClientOriginalName() ;
            $destinationPath = public_path().'/images/' ;
            $file->move($destinationPath,$fileName);

            $blogs->image = '/images/'.$fileName ;
        }
         $blogs->save();

        return  $blogs;
    }
1
  • How to use foreach loop in this code please help me.. Commented Jan 30, 2019 at 10:51

1 Answer 1

2

You can use something like this

if($request->hasFile('images'))
{    
   $data = [];
   foreach($request->file('images') as $image)
   {
      $fileName = $image->getClientOriginalName();
      $destinationPath = public_path().'/images/' ;
      $file->move($destinationPath,$fileName);

      $data[] = '/images/'.$fileName;
   }
 $blogs->image =  json_encode($data);
}
Sign up to request clarification or add additional context in comments.

3 Comments

its called undefined variable data..@Mehran
@Er.Dinesh answer updated. Please check let me know. :)
@Er.Dinesh upvote the answer if it works for you thanks. :)

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.