Reputation: 1146
How can I update the data of a foreach?
The data is rendered like here:
@foreach ($translation as $locale)
<div class="form-group">
<label><strong>Locale</strong></label>
<textarea type="text" class="form-control form-control-sm" id="locale" name="locale" rows="1" disabled>{{$locale->locale}}</textarea>
</div>
<div class="form-group">
<label><strong>Title</strong></label>
<textarea type="text" class="form-control form-control-sm" id="title" name="title" rows="1">{{$locale->title}}</textarea>
</div>
<div class="form-group">
<label><strong>Caption</strong></label>
<textarea type="text" class="form-control form-control-sm" id="caption" name="caption" rows="1">{{$locale->caption}}</textarea>
</div>
@endforeach
And I want to update the values on the data base using a controller function.
I know how to update the single values, but with a foreach, I don't have any idea.
Yesterday I posted another question, but maybe isn't formulated correctly and nobody gives me an answer. I post it here cause maybe you have more information.
Thanks a lot, any help will be really appreciated.
Laravel: How to update array in controller
Upvotes: 0
Views: 4845
Reputation: 1288
You should create a function that allowed to receive array of values
On your view, You need to make all your form element name as array.
@foreach ($translation as $locale)
<div class="form-group">
<label><strong>Locale</strong></label>
<textarea type="text" name="locale[]" rows="1">{{$locale->locale}}</textarea>
</div>
<div class="form-group">
<label><strong>Title</strong></label>
<textarea type="text" name="title[]" rows="1">{{$locale->title}}</textarea>
</div>
<div class="form-group">
<label><strong>Caption</strong></label>
<textarea type="text" name="caption[]" rows="1">{{$locale->caption}}</textarea>
</div>
<input type="hidden" name="locale_id[]" value="{{$locale->id}}"> // Make sure you add the id
@endforeach
Then on your controller, You should handle those arrays
function update(){
$locale_id = $request->locale_id;
$locale = $request->locale ;
$caption = $request->caption;
$title= $request->title;
foreach($locale_id as $key => $value){
DB::table('project_translations')->where('id', $value )->update([
'locale' => $locale[$key],
'title' => $title[$key],
'caption' => $caption[$key],
]);
}
}
Upvotes: 2
Reputation: 21681
I think you can try this :
@foreach ($translation as $locale)
<div class="form-group">
<label><strong>Locale</strong></label>
<textarea type="text" class="form-control form-control-sm" id="locale" name="locale[]" rows="1" disabled>{{$locale->locale}}</textarea>
<input type="hidden" name="locale[]" value="{{$locale->locale}}">
</div>
<div class="form-group">
<label><strong>Title</strong></label>
<textarea type="text" class="form-control form-control-sm" id="title" name="title[]" rows="1">{{$locale->title}}</textarea>
</div>
<div class="form-group">
<label><strong>Caption</strong></label>
<textarea type="text" class="form-control form-control-sm" id="caption" name="caption[]" rows="1">{{$locale->caption}}</textarea>
</div>
<input type="hidden" name="localId[]" value="{{$locale->id}}">
@endforeach
public function update(Request $request){
foreach($request->localId as $key => $value){
DB::table('DbName')->where('id', $value )->update([
'locale' => $request->locale[$key],
'title' => $request->title[$key],
'caption' => $request->caption[$key],
]);
}
}
Hope this work for you !!!
Upvotes: 1
Reputation: 9389
Change your name
attributes to array type like
<textarea type="text" class="form-control form-control-sm" id="locale" name="locale[]" rows="1" disabled>{{$locale->locale}}</textarea>
....
....
And add array input field for id inside loop like,
<input name="id[]" value="{{$locale->id}}" hidden>
Now, In controller post function,
public function update(Request $request){
foreach($request->id as $key => $value){
// Find record to update with Model as your table model
$record = Model::find($value);
$record->locale = $request->locale[$key];
$record->title = $request->title[$key];
$record->caption = $request->caption[$key];
$record->save();
}
}
Hope you understand.
Upvotes: 2
Reputation: 3623
I suppose that your foreach
is surrounded by a <form>
.
I propose this:
name
attributes of your fields into an array with ID of the current locale: name="locale[{{ $locale->id }}][title]"
foreach
to update each locale, like this:$locales = $request->input('locale');
foreach ($locales as $id => $attr)
{
$locale = Locale::find($id);
$locale->title = $attr['title'];
// ...
$locale->save();
}
Upvotes: 1