0

I am uploading profile picture in laravel, In my add.blade.php file I've write like this

<div class="col-md-6">
  <div class="form-group">
    <label for="role">Profile Picture :<span class="danger">*</span> </label>
      <input type="file" class="form-control" id="file" name="file">
  </div>
</div>

in My Controller File my function is like this

$this->validate($request, [
            'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);

    if($request->hasfile('filename'))
    {

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

How can i upload image and save it in my MongoDb database??

2
  • Any error occur in your code?? Commented Mar 19, 2019 at 7:18
  • No error occur, will you please share how to code to upload image in laravel and store in database? Commented Mar 19, 2019 at 7:21

1 Answer 1

2

If you want to multiple images upload then you can try to do this code:

In your blade:

<div class="col-md-6">
  <div class="form-group">
    <label for="role">Profile Picture :<span class="danger">*</span> </label>
      <input type="file" class="form-control" id="file" name="file[]" multiple>
  </div>
</div>

In your Controller:

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

foreach ($images as $key => $image) {

   if ($request->hasFile('file') && $request->file('file')[$key]->isValid()) {
       $path = $request->file[$key]->store('public/images');
       $path = basename($path);

       $image = new Images();
       $image->photo = $path;
       $image->save();
   }
}
Sign up to request clarification or add additional context in comments.

11 Comments

hey Record inserted in database but image isn't add in folder and insert in database too, apart from image i am using other fields too..
You have to link storage folder in your public folder or not?
and image folder's path is this ? /var/www/html/hrm/storage/app/public/imges
you have to run this command php artisan link:storage and to make sure the storage folder linked in your public and then try upload the image.
i got this error ` There are no commands defined in the "link" namespace.`
|

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.