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: https://jsfiddle.net/5tjk7jza/2/. It looks a bit like this, to give you an idea, but the Fiddle will help a lot.:
<tr>
<td><input type="text" id="name[]" size="15" name="name[]"></td>
<td><input type="text" id="heartrate[]" size="1" name="heartrate[]"></td>
<td><input type="text" id="intensity[]" size="1" name="intensity[]"></td>
<td><textarea rows="4" cols="50" id="description[]" name="description[]"></textarea></td>
</tr>
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.
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!
EDIT: What if I just ditched form-model binding and did something like this guy: How to validate multiple records insertion in laravel?. 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.
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...
Here is my form/view:
{!! Form::model($workoutcategories,['method' => 'PATCH', 'url' => 'workoutcategories/update']) !!}
@foreach($workoutcategories as $workoutcategory)
<tr>
<td>{!! Form::text('name[]', $workoutcategory->name, ['class' => 'form-control', 'size' => '15']) !!}</td>
<td>{!! Form::text('heartrate[]', $workoutcategory->heartrate, ['class' => 'form-control', 'size' => '1']) !!}</td>
<td>{!! Form::text('intensity[]', $workoutcategory->intensity, ['class' => 'form-control', 'size' => '1']) !!}</td>
<td>{!! Form::textarea('description[]', $workoutcategory->description,['class' => 'form-control', 'rows' => '4', 'cols'=>'100']) !!}</td>
</tr>
@endforeach
</tbody>
</table>
Here is my controller:
public function CreateOrUpdateCategory ()
{
$workoutcategories = WorkoutCategory::all();
return view('workouts.categoryform', compact('workoutcategories'));
}
public function update(WorkoutCategory $workoutCategory, WorkoutCategoryRequest $request) {
$cats = WorkoutCategory::all();
$workoutCategory->updateorcreate(['name' => $request->name],['name' => $request->name, 'intensity' => $request->intensity, 'heartrate' => $request->heartrate,'description' => $request->description]);
return redirect('workoutcategoies');
}
tr) is a separate record in db then you can update/add only that field by the primary key.updateOrCreatebe like your add/edit?