title: Get item from collection by index Laravel 5
body: <p>I need to upload multiple images to server. If image exists in database update name, upload new. Else create new record in database.
  Script I have uploads new images but doesn't update existing ones.</p>
  
  <pre><code>if ($request-&gt;hasFile('images')) {
      foreach ($request-&gt;file('images') as $key =&gt; $image) {
          $filename = time() . $image-&gt;getClientOriginalName();
          $image-&gt;move('img/products', $filename);
  
          if (isset($product-&gt;images[$key])) {
              $result = $product-&gt;images[$key]-&gt;update([
                  'name' =&gt; $filename
              ]);
              dd($result);
          } else {
              $product-&gt;images()-&gt;create([
                  'name' =&gt; $filename
              ]);
          }
      }
  }
  </code></pre>