how to update an array in laravel - laravel

i have an array of data in a form that i want to update.when i update only the last column updated while the inputs that i updated do change.for example, we have a column size, price, and stock. the size include small,medium, and large and their respective prices and stock number. when i update the price of small size,it doesnt update but rather it updates the medium size.same for the large size also.i havent understood why only a specfic column is updating yet i have added a foreach to loop all the columns when updating.here is my update function.
public function editattributes(Request $request,$id)
{
$merchadisedata=Merchadise::select('id','merch_name','merch_code','merch_image')->find($id);
if ($request->isMethod('post')){
$data=$request->all();
foreach($data['attrid'] as $key=>$attr){
if(!empty($attr)){
Productattribute::where(['id'=>$data['attrid'][$key]])
->update([
'productattr_price'=>$data['productattr_price'][$key],
'productattr_stock'=>$data['productattr_stock'][$key],
]);
}
$message='Product attributes have been updated successfully';
Session::flash('success_message',$message);
return redirect()->back();
}
}
return view('backend.merchadise.editproductattributes')->with(compact('merchadisedata'));
}
on dd($data);die(); i get the inputs i have filled
here is the form in the blade file that am submitting
<form method="post" action="{{ url('admin/edit-attributes/'.$merchadisedata->id) }}">
{{csrf_field()}}
<table id="products" class="table table-striped table-bordered nowrap" style="width:100%;">
<thead>
<tr>
<th>Attribute Size</th>
<th>Attribute Stock</th>
<th>Attribute Price</th>
<th>Attribute Sku</th>
</tr>
</thead>
<tbody>
#foreach ( $merchadisedata->merchadiseattributes as $attribute)
<input type="text" name="attrid[]" value="{{ $attribute->id }}" style="display: none"/>
<tr>
<td>{{ $attribute->productattr_size}}</td>
<td>
<input type="number" name="productattr_stock[]" value="{{ $attribute->productattr_stock }}" required=""/>
</td>
<td>
<input type="number" name="productattr_price[]" value="{{ $attribute->productattr_price }}" required=""/>
</td>
<td>{{ $attribute->productattr_sku }}</td>
</tr>
#endforeach
</tbody>
</table>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sm btn-block">Submit</button>
</div>
</form>
where might i be going wrong in my code

Related

Laravel vue js 3 need to edit single record from table

I have this following:
<div class="modal-body" >
<form v-for="lead in editleads" id="" class="form-horizontal validate-form">
<input v-model="editl" type="text" class="form-control my-2 py-2" :placeholder="lead">
</form>
</div>
<table class="table table-hover align-middle mb-0">
<thead class="">
<tr>
<th><input type="checkbox" class="form-check-input" v-model="selectAll" title="Select All"></th>
<th v-for="(header, index) in visibleHeaders" :key="index" scope="col">
{{ header }}
</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr v-show="leads.length" v-for="(column, index) in visibileColumn" :key="index">
<td>
<input type="checkbox" class="form-check-input" v-model="selected" :value="column.id" />
</td>
<td v-for="atr in column">
{{atr}}
</td>
<td>
<button #click="editLead(column.id)" type="button" class="btn btn-sm btn-secondary" data-mdb-toggle="modal" data-mdb-target="#editLeadModal" >
<i class="fa-solid fa-pen-to-square"></i>
</button>
</td>
</tr>
<tr v-show="!leads.length">
<td colspan="12" class="text-center">Sorry :( No data found.</td>
</tr>
</tbody>
</table>
data() {
reurn {
headers: [],
leads: [],
...
}
},
editLead(id) {
axios.get('/leads/'+id+'/edit')
.then(response => {
this.editleads = response.data.leads
})
},
I want to edit each table record one by one. I mean to edit single record, but the problem is i return an array object from backend and i dont know how to edit each input. If i click now on form all input filled with same value. can someone help with this

How to get Laravel Blade value as an Array data using Post Method?

I Have a Blade Form that will execute Post Method, this is my blade
#foreach ($dataku as $row => $order)
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12 text-center">
<b>{{ $order->delivery_order_no }}</b>
<input type="hidden" name="order[{{ $row }}][do_id]" value="{{ $order->id }}">
<input type="hidden" name="order[{{ $row }}][so_id]" value="{{ $order->sales_order->id }}">
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="table-responsive m-t-40" style="clear:both;">
<table class="table table-hover" style="font-size: 9pt;">
<thead>
<tr><th class="text-center">No</th>
<th class="text-center">SKUID</th>
<th class="text-center">Item Name</th>
<th class="text-center">UOM</th>
<th class="text-center">Qty So</th>
<th class="text-center">Qty Do</th>
<th class="text-center">Qty Confirm</th>
<th class="text-center">Qty Minus</th>
<th class="text-center">Remark Confirm</th>
</tr>
</thead>
<tbody>
#foreach ($order->delivery_order_details as $do =>$detOrder )
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $detOrder->skuid }}</td>
<td>{{ $detOrder->sales_order_detail->item_name }}</td>
<td>{{ $detOrder->uom->name }}</td>
<td>{{ $detOrder->sales_order_detail->qty }}</td>
<td>{{ $detOrder->qty_do }}</td>
<td>
<input type="hidden" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][skuid]]" value="{{ $detOrder->skuid}}">
<input type="number" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][qty_do]]" value="{{ $detOrder->qty_do }}">
</td>
<td>
<input type="number" min="0" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][qty_minus]]" value="0">
</td>
<td>
<input type="text" placeholder="Remark Confirm" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][remarks]]">
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endforeach
I Just want to get the data from the blade as an array ,... and then this is my controller
public function update(Request $request)
{
return $request->all();
}
I Get The Data Like This
is the return value from my blade $request->all(); correct? looks like something wrong ???
#sta provided an answer but I would make it more detailed:
When you build more complex structure (nested) in a html form input, every next key or list has to be surrounded by [].
So if you do just a nested keys it will be something[key1][key2][key3].
And if you do an array and a nested keys it will be something[key1][key2][][key3], where [] means that [key2] will be an array (and every array element will have a key3 key with provided value in input value attribute.
That's why this input name can't work:
order[0][detail[][0][skuid]]
but this one will work:
name="order[0][detail][0][qty_minus]

How to get Selected checkbox into database

I have a page for purchase order in that page i show data from table obat and i want to get selected checkbox using that data into table purchase order
This is my table obat
<table class="table table-bordered" style="margin-top:20px">
<tbody>
<tr class="text-center">
<th>No</th>
<td>Kode Obat</td>
<td>Nama Obat</td>
<td>Harga</td>
</tr>
#foreach ($obat as $key =>$o)
<tr>
<th class="text-center">
#foreach ($po as $po)
<input type="checkbox" name="select" id="select">
#endforeach
</th>
<td>
<input type="text" class="form-control" value="{{ $o->kode_obat }}">
</td>
<td>
<input type="text" class="form-control" value="{{ $o->nama_obat }}">
</td>
<td>
<input type="text" class="form-control" value="{{ $o->harga_obat }}">
</td>
</tr>
#endforeach
</tbody>
</table>
The checkbox(select) is from table purchase order but it can't show. if i did't use foreach its show
you can pass array also to check if it is in the array or not then you can this like below.
in your controller
public function edit($id)
{
$odat=Odat::all();
foreach($odat as $od)
{
$listod[]=$odat->select;
}
return view('yourbladefile',compact('roles','listod'));
}
then in your view
<input type="checkbox" name="select" #if (in_array(yourpurchaseordernamefromdb, $listod))
{{'checked'}} #endif>Selected</td>
personally i managed liked this. hope it helps

Uploading image can't saved at directory and having error "htmlspecialchars() expects parameter 1 to be string, array given "

i have a view input like that :
<form class="form-group" action="/user6" method="post" enctype="multipart/form-data">
<table class="table table-striped">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th style="width: 60px">Tidak Baik</th>
<th style="width: 60px">Baik</th>
</tr>
<div class="form-group">
<tr>
<td>1.</td>
<td><input class="form-control" style="border:none" type="text" name="question1" value="Kondisi, kebersihan, pelumasan bearing" readonly></td>
{{-- <td><input class="form-control" type="text" placeholder=".input-lg"></td> --}}
<td>
<label><input type="radio" name="answer1" value="tidak baik" checked></label>
</td>
<td>
<label><input type="radio" name="answer1" value="baik"></label>
</td>
</tr>
<tr>
<td></td>
<td> <input class="form-control" style="border:none" type="file" name="image" value="" readonly> </td>
</tr>
</div>
<tr>
<td></td>
<td> <div class="form-group">
<label>Catatan</label>
<textarea class="form-control" name="catatan" rows="3" placeholder="Enter ..." required></textarea>
</div>
</tr>
<input type="hidden" name="alat_id" value="7">
<input type="hidden" name="status" value="3 Bulanan">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</tbody>
</table>
<div class="box-footer">
<button type="submit" class="btn btn-primary" name="name" value="post">Submit</button>
</div>
</form>
and to save this i have controller :
public function store6(Request $request)
{
if($request->hasfile('image'))
{
foreach($request->file('image') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan']));
$user->catatan = $request->catatan;
$user->status = $request->status;
$user->save();
//dd($user);
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
before i add "enctype="multipart/form-data" , my view dont have error BUT cant display and saved image at directory . i want saved image to directory but cant saved .
i create a folder named 'images' at public . after i add enctype="multipart/form-data" . this view having error "htmlspecialchars() expects parameter 1 to be string, array given "
its my view after input :
<table class="table table-condensed">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th>Hasil</th>
{{-- <th style="width: 40px">Label</th> --}}
</tr>
<tr>
<td>1.</td>
<td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['answer1'] }}
</td>
</tr>
<tr>
<td>2.</td>
<td>{{ $pemeliharaan->pertanyaan['image'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['image'] }}
</td>
</tr>
<td><img src="{{ url('images/'.$pemeliharaan->pertanyaan['image'])}}"></td>
</tbody></table>
Something in your table is array, it's not a string so when you put it inside {{}}, it will show error.
You should check
$pemeliharaan->pertanyaan['question1']
$pemeliharaan->pertanyaan['answer1']
$pemeliharaan->pertanyaan['image']
by using dd() function.
Deal with one problem at a time. At present you are storing the image but not saving the path to the file, which you saved in $data. You are not using $data anywhere.
Then you have a problem rendering the new model in the view. This is a totally different issue and is caused by having non-string data passed to the htmlspecialchars function which is what {{ }} does.

Form submit button in table without function

Can anyone explains me, why the submit buttons no and yes in the following code not trigger?
Without the table, it works.
<table class="table">
<thead>
<tr>
<th scope="col">Group</th>
<th scope="col">Yes No</th>
</tr>
</thead>
#foreach($invitations as $invitation)
{!! Form::open(array('route'=>'store.groupentry')) !!}
<tbody>
<tr>
<td>
Do you want to enter group {{$invitation->group_name}}?
</td>
<td>
<input type="hidden" name="idgroup" value="{{ $invitation->idgroup }} "/>
<input type="hidden" name="groupname" value="{{ $invitation->group_name }} "/>
<button type="submit" class="btn btn-default" name = "submitbutton" value = "save">Yes</button>
<button type="submit" class="btn btn-default" name = "submitbutton" value = "nosave">No</button>
{{ csrf_field() }}
</td>
</tr>
</tbody>
{!! Form::close() !!}
#endforeach
</table>
Maybe not valid html generation. You generate many forms wrap many tbody in one table. I think you need create one form, and before submit set hidden values with js

Resources