title: Looping through input array in Laravel 5
body: <p>I have a basic UI where users can add a simple list with a label and a value. I want to loop through that list to store the data in a "Detail" model.</p>
  
  <p>I have the following code.</p>
  
  <p>Controller:</p>
  
  <pre><code>$details = $request-&gt;input('detail_label');
  
  foreach($details as $key =&gt; $value)
  {
      if(!empty($request-&gt;input('detail_value.'.$key))) {
  
          // if the detail has an existing ID
          if($request-&gt;input('detail_id.'.$key)) {
              $detail = Detail::find($request-&gt;input('detail_id.'.$key));
          } else {
              $detail = new Detail;
          }
  
          $detail-&gt;type = $request-&gt;input('detail_type.'.$key);
          $detail-&gt;label = $request-&gt;input('detail_label.'.$key);
          $detail-&gt;value = $request-&gt;input('detail_value.'.$key);
  
          if($request-&gt;input('detail_privacy.'.$key) == 1) {
              $detail-&gt;privacy = 1;
          } else {
              $detail-&gt;privacy = 0;
          }
  
          $user-&gt;details()-&gt;save($detail);
  
      }
  
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@foreach($user-&gt;details as $detail)
      &lt;div class="detail"&gt;
  
          &lt;input type="hidden" name="detail_id[]" value="{{ $detail-&gt;id }}"&gt;
  
          &lt;label&gt;Type
          &lt;select name="detail_type[]"&gt;
              &lt;option @if($detail-&gt;type == '1')selected @endif value="1"&gt;Phone&lt;/option&gt;
              &lt;option @if($detail-&gt;type == '2')selected @endif value="2"&gt;Phone (mobile)&lt;/option&gt;
              &lt;option @if($detail-&gt;type == '3')selected @endif value="3"&gt;URL&lt;/option&gt;
              &lt;option @if($detail-&gt;type == '4')selected @endif value="4"&gt;Email&lt;/option&gt;
          &lt;/select&gt;
          &lt;/label&gt;
  
          &lt;label&gt;Label
          &lt;input type="text" name="detail_label[]" value="{{ $detail-&gt;label }}"&gt;
          &lt;/label&gt;
  
          &lt;label&gt;Value
          &lt;input type="text" name="detail_value[]" value="{{ $detail-&gt;value }}"&gt;
          &lt;/label&gt;
  
          &lt;label&gt;Private?
          &lt;input type="checkbox" name="detail_privacy[]" @if($detail-&gt;privacy == true) checked @endif value="1"&gt;
          &lt;/label&gt;
  
          &lt;label&gt;Delete?
          &lt;input type="checkbox" name="detail_delete[]" value="{{ $detail-&gt;id }}"&gt;
          &lt;/label&gt;
  
      &lt;/div&gt;&lt;!-- / detail --&gt;
  @endforeach
  </code></pre>
  
  <p>Every aspect of my code works as I had planned except the detail_privacy field. It sets and unsets the boolean privacy attribute but it pays no attention to which $key I want. It always sets it according to the order in the loop. If I just set one detail to be private it will be first. If I set two, (whichever two), it will be the first and second.</p>
  
  <p>Something is clearly wrong with my logic but I can't tell what. </p>
  
  <p>Any help would be really appreciated. Thanks!</p>