Exception or error:
I want to update multiple rows in a table, I tried a lot but could not do that. I see different thread to find solution but not find. Here is below I share my controller code, edit blade code.
Here is Controller code:
the Second part of this Code is not update the records from this line $c=count($data['customer']);
;
public function update(Request $request)
{
$data = $request->all();
$empid = Session::get('emp_id');
$empname = Session::get('user_name');
$id = $data['id'];
$exhibition_type = $data['exhibition_type'];
// $trndate = $data['trndate'];
$season = $data['season'];
$meeting_date = $data['meeting_date'];
$update_exhibition = DB::update("update EXHIBITION_FABRIC_HEADERS set EXHB_TYPE_ID='$exhibition_type', SEASON_ID = '$season', MEETING_DATE ='$meeting_date', LAST_UPDATE_BY ='$empid', LAST_UPDATE_BY_NAME = '$empname' where trnno = '$id'");
$c=count($data['customer']);
for ($i=0;$i<$c;$i++) {
$customer = $data['customer'][$i];
dd($customer);
$category = $data['category'][$i];
$fabric_code = $data['fabric_code'][$i];
$selection = $data['selection'][$i];
$update_exhibition = DB::update("update EXHIBITION_FABRIC_LINES set CUSTOMER_ID = '$customer', FABRIC_CATEGORY_ID='$category', FABRIC_CODE = '$fabric_code', SELECTION_FLAG ='$selection', LAST_UPDATE_BY ='$empid', LAST_UPDATE_BY_NAME = '$empname' where trnno = '$id'");
}
if ($update_exhibition == true)
{
return response()->json(['result' => 'success', 'trnno' => $id]);
}
else
{
return response()->json(['result' => 'error']);
}
}
Here is The edit.blade.php:
<form method="post" name="exhibition_form" id="exhibition_form" action="">
{{ csrf_field() }}
<div class="row">
<input type="hidden" name="id" id='id' value="{{ $exhibition_headers[0]->trnno }}">
<!-- @php
$max = DB::select("select nvl(max(TRNNO),0)+1 ID from EXHIBITION_FABRIC_HEADERS");
$trnno = $max[0]->id;
$trn_date = date('Y-m-d');
@endphp -->
<div class="col-md-2 form-group mb-3">
<label for="doc">Doc #</label>
<input type="number" class="form-control" id="doc" name="doc" value="{{ $exhibition_headers[0]->trnno }}" placeholder="Doc #" readonly="true">
</div>
<div class="col-md-2 form-group mb-3">
<label for="doc">Doc Date</label>
<input type="text" class="form-control" id="trn_date" name="trn_date" value="{{ $exhibition_headers[0]->trndate }}" placeholder="Doc Date" readonly="true">
</div>
<div class="col-md-2 form-group mb-3">
<label for="exhibition_type">Exhibition Type</label>
<select class="form-control" name="exhibition_type" id='exhibition_type'>
<option value="">Select Exhibition Type</option>
@foreach($exhibition_type as $exhibition)
<option value="{{$exhibition->exhb_type_id}}" <?php if($exhibition->exhb_type_id==$exhibition_headers[0]->exhb_type_id){ echo 'selected';} ?>>{{ $exhibition->exhb_type_name}}</option>
@endforeach
</select>
</div>
<div class="col-md-2 form-group mb-3">
<label for="season">Season</label>
<select class="form-control" name="season" id='season'>
<option value="">Select Season</option>
@foreach($season as $sea)
<option value="{{$sea->season_id}}" <?php if($sea->season_id==$exhibition_headers[0]->season_id){ echo 'selected';} ?>>{{ $sea->season_name}}</option>
@endforeach
</select>
</div>
<div class="col-md-3 form-group mb-3">
<label for="meeting_date">Meeting Date</label>
<input type="date" class="form-control" id="meeting_date" name="meeting_date" placeholder="Enter Meeting Date" value="{{ date('Y-m-d',strtotime($exhibition_headers[0]->meeting_date)) }}">
</div>
</div>
<div class="row">
@foreach($exhibition_lines as $key => $exhibition_line)
<div class="col-md-3 form-group mb-3">
<label for="customer">Customer</label>
<select class="form-control" name="customer[]" id='customer'>
<option value="">Select Customer</option>
@foreach($customers as $customer)
<option value="{{$customer->customer_id}}" <?php if($customer->customer_id==$exhibition_lines[$key]->customer_id){ echo 'selected';} ?> >{{ $customer->customer_name}}</option>
@endforeach
</select>
</div>
<div class="col-md-3 form-group mb-3">
<label for="category">Category</label>
<select class="form-control" name="category[]" id='category'>
<option value="">Select Category</option>
@foreach($categories as $category)
<option value="{{$category->fabric_category_id}}" <?php if($category->fabric_category_id==$exhibition_lines[$key]->fabric_category_id){ echo 'selected';} ?> >{{ $category->fabric_category_name}}</option>
@endforeach
</select>
</div>
<div class="col-md-3 form-group mb-3">
<label for="fabric_code">Fabric Code</label>
<select class="form-control" name="fabric_code[]" id='fabric_code'>
<option value="">Select Fabric Code</option>
@foreach($fabric_codes as $fabric_code)
<option value="{{$fabric_code->fabric_code}}" <?php if($fabric_code->fabric_code==$exhibition_lines[$key]->fabric_code){ echo 'selected';} ?> >{{ $fabric_code->fabric_code}}</option>
@endforeach
</select>
</div>
<div class="col-md-1 form-group mb-1">
<label for="selection">Selection</label>
<input type="hidden" name="selection[]" id="selection_0" value="off">
<input type="checkbox" class="form-control re_selection" data-id='0' id="re_selection_0" name="re_selection[]" onclick="get_selection('0')" <?php if($exhibition_lines[0]->selection_flag == 'on'){ echo 'checked'; } ?> >
</div>
<div class="col-md-1 form-group mb-1">
<br>
<a style="padding: 4px;color:#fff" class="btn btn-success btn-sm" type="button" onclick="add_more();"><i class="text-18 i-Add"></i></a>
<a style="padding: 4px;color:#fff" class="btn btn-danger btn-sm" type="button" onclick="remove_row();"><i class="text-18 i-Remove"></i></a>
</div>
@endforeach
</div>
<div id="add_here"></div>
<div class="col-md-2 form-group mb-3">
<label></label>
<a class="btn btn-success" id="submit" onclick="exhibition_update();">Save</a>
<!-- <input type="submit"> -->
</div>
</form>
Here is the route.php:
Route::post('/update_exhibition', 'ExhibitionController@update')->name('update_exhibition')-
>middleware('checksession');
Here i will share my edit form screenshot below
It can save the last record in all three row in database.
please tell me the solution of this problem where i can make a mistake. Thanks in Advance.
How to solve: