title: Laravel Tables/Create and Edit in same form?
body: <p>Hoping someone can help me. I'm used to procedural code, and I'm currently refactoring an older PHP app into Laravel. A page in the old app had a table layout with each td having an input. These inputs were all an array, and with jquery, you could add a new row to the table to create a new record. Here's a jsfiddle: <a href="https://jsfiddle.net/5tjk7jza/2/" rel="nofollow noreferrer">https://jsfiddle.net/5tjk7jza/2/</a>. It looks a bit like this, to give you an idea, but the Fiddle will help a lot.:</p>
  
  <pre><code>&lt;tr&gt;
    &lt;td&gt;&lt;input type="text" id="name[]" size="15" name="name[]"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="text" id="heartrate[]" size="1" name="heartrate[]"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="text" id="intensity[]" size="1" name="intensity[]"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;textarea rows="4" cols="50" id="description[]" name="description[]"&gt;&lt;/textarea&gt;&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>When it saves, it runs through the loop updating each of the old records based on its id in the array up to a max specified by a hidden input at the bottom. Then it inserts the new records.</p>
  
  <p>I'd like to do this using Laravel and form model binding, and not have separate views. I really like this add/edit in the same table/view. For the life of me, despite reading docs and google searching, I can't find a solution. I've looked at syncing, but that is only for many-to-many relationships/pivot tables, an attach won't work, and a save won't work. Can anyone help me out? Thanks!! Loving Laravel so far!</p>
  
  <p>EDIT: What if I just ditched form-model binding and did something like this guy: <a href="https://stackoverflow.com/questions/37881490/how-to-validate-multiple-records-insertion-in-laravel">How to validate multiple records insertion in laravel?</a>. Is that 'wrong'? I could loop through all the "old" records and then insert all the new ones, but that feels a lot like not using the framework and procedural code.</p>
  
  <p><strong>EDIT 2: Having some trouble with updateorcreate method... I get an error "preg_replace(): Parameter mismatch, pattern is a string while replacement is an array." Can anyone look at this and help me? I've been looking at the docs for a few hours now...</strong></p>
  
  <p>Here is my form/view:</p>
  
  <pre><code>{!! Form::model($workoutcategories,['method' =&gt; 'PATCH', 'url' =&gt; 'workoutcategories/update']) !!}
  
  @foreach($workoutcategories as $workoutcategory)
  &lt;tr&gt;
      &lt;td&gt;{!! Form::text('name[]', $workoutcategory-&gt;name, ['class' =&gt; 'form-control', 'size' =&gt; '15']) !!}&lt;/td&gt;
      &lt;td&gt;{!! Form::text('heartrate[]', $workoutcategory-&gt;heartrate, ['class' =&gt; 'form-control', 'size' =&gt; '1']) !!}&lt;/td&gt;
      &lt;td&gt;{!! Form::text('intensity[]', $workoutcategory-&gt;intensity, ['class' =&gt; 'form-control', 'size' =&gt; '1']) !!}&lt;/td&gt;
      &lt;td&gt;{!! Form::textarea('description[]', $workoutcategory-&gt;description,['class' =&gt; 'form-control', 'rows' =&gt; '4', 'cols'=&gt;'100']) !!}&lt;/td&gt;
  &lt;/tr&gt;
      @endforeach
  
  &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Here is my controller:</p>
  
  <pre><code>public function CreateOrUpdateCategory ()
  {
      $workoutcategories = WorkoutCategory::all();
      return view('workouts.categoryform', compact('workoutcategories'));
  }
  
  public function update(WorkoutCategory $workoutCategory, WorkoutCategoryRequest $request) {
      $cats = WorkoutCategory::all();
      $workoutCategory-&gt;updateorcreate(['name' =&gt; $request-&gt;name],['name' =&gt; $request-&gt;name, 'intensity' =&gt; $request-&gt;intensity, 'heartrate' =&gt; $request-&gt;heartrate,'description' =&gt; $request-&gt;description]);
      return redirect('workoutcategoies');
  }
  </code></pre>