Thank you in advance for the assistance.
I have a table in my database. Migration.
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->integer('group');
$table->string('code');
$table->string('name');
$table->double('price');
$table->timestamps();
});
I retrieve all the products from the database and sort them according to the group in my view. The input name = "{{id}}" and input value = "{{price}}"
Please see my form:
<form method="POST" enctype="multipart/form-data" action="{{route('product.update')}}">
@csrf
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="pills-ns_890-tab" data-toggle="pill" href="#ns_890_main"
role="tab" aria-controls="pills-ns_890" aria-selected="true">KX-NS890</a>
</li>
</ul>
<div class="tab-content" id="pills-tabContent">
<div class="tab-pane fade show active" id="ns_890_main" role="tabpanel" aria-labelledby="pills-ns_890-tab">
@foreach($main_890 as $item)
<div class="form-group row">
<label for="{{$item->id}}" class="col-md-4 col-form-label text-md-right">{{$item->code. ' ' .$item->name}}</label>
<div class="col-md-6">
<div >
<input name="{{$item->id}}" type="number" min="0" id="{{$item->id}}" class="form-control @error('{{$item->id}}') is-invalid @enderror"
value="{{$item->price}}" autocomplete="{{$item->id}}" required autofocus>
</div>
</div>
</div>
@endforeach
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Save and Continue') }}
</button>
</div>
</div>
</form>
After the form is submitted I get the following when I dd($request) in my Controller.
+request: Symfony\Component\HttpFoundation\ParameterBag {#44 ▼
#parameters: array:187 [▼
"_token" => "XdnsoU9sqtgYpwFAqGQRM2fTde6mYJhhwJL3a3R2"
119 => "8461"
120 => "9682"
121 => "501"
122 => "2008"
123 => "16141"
124 => "5385"
125 => "9122"
126 => "3310"
127 => "1692"
128 => "2811"
129 => "1686"
130 => "2811"
131 => "1506"
132 => "1064"
133 => "4078"
134 => "8329"
And dd($request) ends with the following:
115 => "115"
116 => "2660"
117 => "730"
118 => "8400"
28 => "1200"
29 => "75"
30 => "95"
31 => "68"
32 => "243"
33 => "474"
202 => "56"
203 => "1990"
204 => "760"
205 => "760"
206 => "115"
The reason I am including the last part of the dd($request), is that when the form is submitted it would take the value assigned to 119 which is 8461 but in the database updates the record where id is 206, the last entry received from my form, so 206 => "115" becomes 206 => "8461", this is the only record then updated in the database.
What I am trying to do is update the price column by id with the value received from the form.
Please see my code below:
$products = collect($request->except(['_token']))
->mapWithKeys(function($item, $key) {
return [$key => ['price' => $item]];
})->all();
foreach ($products as $product){
$id = collect($request->except(['_token']))->mapWithKeys(function ($item){
return ['id' => $item];
});
DB::table('products')->
where('id', $id)->
update(array('price' => $product['price']));
Please be so kind as to let me know where I have fallen off of the wagon?
It can be simplified a lot like so:
collect($request->except(['_token']))->each(static function($item, $key) {
return DB::table('products')->where('id', $key)->update([
'price' => $item
]);
})
There is no need to do overcomplicate things with all this mapping you are doing, which ultimately leads to your issue you are experiencing. My guess is that the order of the mapping/request data is not the same, so when you do the second mapWithKeys in the foreach, it does not match with the current $product you are iterating over.
Hope this helps a bit.
Loop the data and use Eloquent models to update the data. As i helped you in the other question, mapWithKeys() was to combat pivot table updates, you should often not use that one. As it is for very special conditions, here you can just loop it. I would always prefer eloquent models over DB::update().
foreach ($request->except(['_token']) as $id => $price) {
$product = Product::find($id);
$product->price = $price;
$product->save();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With