title: Upload and retrieve image in Laravel
body: <p>I need to save the image for an avatar.</p>
  
  <p>Can anyone give me a simple code to save and retrieve image?</p>
  
  <p>I need to:</p>
  
  <ul>
  <li>Save image in folder </li>
  <li>Save image name in DB </li>
  <li>Finally retrieve on image tag; I have to do it by Query Builder </li>
  </ul>
  
  <p><strong>Form:</strong></p>
  
  <pre><code> &lt;form action="" method="post" role="form" multiple&gt;
          {{csrf_field()}}
          &lt;legend&gt;Form Title&lt;/legend&gt;
  
          &lt;div class="form-group"&gt;
              &lt;label for=""&gt;Your Image&lt;/label&gt;
              &lt;input type="file" name="avatar"&gt;
          &lt;/div&gt;
          &lt;button type="submit" class="btn btn-primary"&gt;save&lt;/button&gt;
          &lt;a href="{{'/'}}" class="btn btn-primary"&gt;back&lt;/a&gt;
       &lt;/form&gt;
              &lt;img name="youravatar" src=""&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p><strong>Route:</strong></p>
  
  <pre><code>Route::get('pic','avatarController@picshow');
  
  Route::post('pic','avatarController@pic');
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <p>I have the <code>avatarController</code>, but it is empty because I don't know what to do.</p>
  
  <p><strong>Database:</strong> </p>
  
  <p>Table name: avatar<br>
  Fields: name id, imgsrc, created_at, Updated_at</p>
  
  <p><strong>Other:</strong> </p>
  
  <p>I found this code but I can't find out anything:</p>
  
  <pre><code> if ($request-&gt;hasFile('avatar')) {
        $file = array('avatar' =&gt; Input::file('avatar'));
        $destinationPath = '/'; // upload path
        $extension = Input::file('avatar')-&gt;getClientOriginalExtension();
        $fileName = rand(11111,99999).'.'.$extension; // renaming image
        Input::file('avatar')-&gt;move($destinationPath, $fileName);
    }
  </code></pre>