Wednesday, March 1, 2017

how to upload and retrieve image on laravel

I need to save image for an avatar and I don't know how to do it.

Can anyone give me a simple code to save and retrieve image?

I need to:

  • Save image on folder
  • Save image name at DB
  • Finally retrieve on image tag? I have to do it by Query Builder

Form:

 <form action="" method="post" role="form" multiple>
        
        <legend>Form Title</legend>

        <div class="form-group">
            <label for="">Your Image</label>
            <input type="file" name="avatar">
        </div>
        <button type="submit" class="btn btn-primary">save</button>
        <a href="/" class="btn btn-primary">back</a>
     </form>
            <img name="youravatar" src="">
        </div>

Route:

Route::get('pic','avatarController@picshow');

Route::post('pic','avatarController@pic');

Controller:

I've the avatarController but is empty because I don't know what to do.

Database:

Table name: avatar Fields: name id, imgsrc, created_at, Updated_at

Thanks in advance

Other:

I found this code but I can't find out anything

 if ($request->hasFile('avatar')) {
      $file = array('avatar' => Input::file('avatar'));
      $destinationPath = '/'; // upload path
      $extension = Input::file('avatar')->getClientOriginalExtension();
      $fileName = rand(11111,99999).'.'.$extension; // renaming image
      Input::file('avatar')->move($destinationPath, $fileName);
  }




via siros

Advertisement