Laravel - saving many rows in one request, possible?
Laravel - saving many rows in one request, possible?
I have the following blade:
<form method="post" action="{{ route('update-document') }}">
{{csrf_field()}}
@foreach($customerDocuments as $document)
{!! Form::select('documents',['approved'=>'Approved', 'rejected'=>'Rejected', 'uploaded'=>'Uploaded'], $document->status, ['class' => 'form-control']) !!}
@endforeach
{!! Form::submit('Save', ['class=', '"btn btn-primary"']); !!}
</form>
I have about 4 rows where each of row has a select field.
I want my update method to save all rows at once.
The issue is that Laravel's $request by default knows how to update one row, not many.
$request
Is there a way to pass all data from all rows into my $request object?
$request
1 Answer
1
Put the save button outside of your foreach loop, give each select a reference to the object it updates, and in your controller method parse through $request to update the correct objects. I find it easier to use AJAX and send JSON to describe something like that. For instance the ID of your select could be the record ID you want to update. Your JS can then build the JSON and use the ID as the key for a dictionary with all of the values that need to be updated for that row. There may be a way to do this using blade syntax but I am not aware of it.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.