我有一张桌子,在桌子的里面,我有一个文本框,用于查看页面。
现在的问题是,每当我更新特定的行或ID时,它就会更新所有内容。如何只更新特定的行或ID而又不更新所有内容?非常感谢你
这是我的控制器
public function updateSchedule(Request $request, $id)
{
$timein = $request->input('timeIn');
$timeout = $request->input('timeOut');
DB::table('schedules')
->update(['time_in' => $timein, 'time_out' => $timeout]);
$notification = array(
'message' => 'Employee Time Updated!',
'alert-type' => 'success'
);
return redirect()->back()->with($notification, 'Employee Time Updated!');
}
这是我的观点
{!! Form::open(['action' => ['Admin\EmployeeFilemController@updateSchedule', $employee->id], 'method' => 'POST']) !!}
<div class="row">
<div class="form-group col-md-12">
<small>Employee No. and Name: </small><b><i> {{ $employee->employee_no }} : {{ $employee->last_name }}, {{ $employee->first_name }}</i></b>
<input type="hidden" name="hidEmployeeno" value='<?php echo $employee->employee_no ?>'>
<input type="hidden" name="hidEmployeeLast" value='<?php echo $employee->last_name ?>'>
<input type="hidden" name="hidEmployeeFirst" value='<?php echo $employee->first_name ?>'>
<hr>
</div>
</div>
<table class="table">
<thead>
<tr>
<th>DATE</th>
<th>TIME IN</th>
<th>LUNCH</th>
<th>TIME OUT</th>
<th>TOTAL HOURS</th>
<th>STATUS</th>
</tr>
</thead>
<tbody>
@foreach ($employeeSched as $setTime)
<tr>
<td> {{Form::label('date_today', $setTime->date_today,['class'=>'form-control'])}}</td>
<td><input type="time" name="timeIn" class="form-control col-md-10" value='{{ $setTime->time_in }}'></td>
<td><p>12:00 PM - 1:00 PM</p></td>
<td><input type="time" name="timeOut" class="form-control col-md-10" value='{{ $setTime->time_out }}'></td>
<td>@php
$time1 = strtotime($setTime->time_in);
$time2 = strtotime($setTime->time_out);
$difference = round(abs($time2 - $time1) / 3600,2);
$total = $difference - 1;
echo '<b>' . $total . '</b>';
@endphp</td>
<td>
@php
if ($total < 8) {
echo '<b>Late</b>';
} else if($total > 8) {
echo '<b>OT</b>';
} elseif ($total < 8.5 && $total == 8) {
echo '<b>In</b>';
}
@endphp
</td>
</tr>
@endforeach
</tbody>
</table>
{{Form::button('<i class="fa fa-clock"> UPDATE TIME</i>',['type' => 'submit','class' => 'btn btn-info btn-sm', 'style'=>"display: inline-block;"])}}
{{Form::hidden('_method', 'PUT')}}
{!! Form::close() !!}
谢谢您的帮助,谢谢
答案 0 :(得分:1)
由于您必须更改多个计划timeIn
和timeOut
,因此需要通过计划ID更新员工的计划时间。如下更新代码。我希望所提供的更改是不言自明的。
刀片(仅必需的部分)
<td><input type="time" name="schedules[{{ $setTime->id }}][timeIn]" class="form-control col-md-10" value='{{ $setTime->time_in }}'></td>
<td><input type="time" name="schedules[{{ $setTime->id }}][timeOut]" class="form-control col-md-10" value='{{ $setTime->time_out }}'></td>
注意:我假设
$setTime->id
是计划ID
控制器
public function updateSchedule(Request $request, $id)
{
$schedules = $request->get('schedules');
foreach ($schedules as $id => $schedule) {
DB::table('schedules')
->where('id', $id)
->update([
'time_in' => $schedule['timeIn'],
'time_out' => $schedule['timeOut']
]);
}
$notification = array(
'message' => 'Employee Time Updated!',
'alert-type' => 'success'
);
return redirect()->back()->with($notification, 'Employee Time Updated!');
}
注意:即使未修改所有记录,此实现也会占用DB n(计划记录数)时间。
提示:您可以将旧值与可修改的值一起传递以进行比较,以进行比较,并仅对已修改的值执行更新查询。
答案 1 :(得分:0)
您需要在查询中添加$ id,这就是它更新整行的原因:
DB::table('schedules')->where('id', $id)->update(['time_in' => $timein, 'time_out' => $timeout]);
此外,我将建议使用Eloquent。
答案 2 :(得分:0)
您可以使用where子句(例如)按ID选择。
DB::table('schedules')->where('id', $id)
->update(['time_in' => $timein, 'time_out' => $timeout]);
此外,如果一切正常,也可以尝试事务,或者在出现问题时捕获错误。
DB::beginTransaction();
try {
DB::table('schedules')->where('id', $id)
->update(['time_in' => $timein, 'time_out' => $timeout]);
DB::commit();
} catch (\Exception $e) {
DB::rollback();
print_r($e);die;
}
答案 3 :(得分:0)
您也可以尝试这种方式。
DB::update('update schedules set time_in = "'.$timein.'", time_out = "'.$timeout.'" where id = "'.$id.'");
答案 4 :(得分:0)
您没有告诉数据库您要更新的行。当您编写查询时,是“ update'table'set'col_name'='value'“;但是您需要告诉它要更新的ID。就像'where'id'= 12'。 希望你能理解。 :)
public function updateSchedule(Request $request, $id){
$timein = $request->input('timeIn');
$timeout = $request->input('timeOut');
DB::table('schedules')->where('id', $id)->update(['time_in' => $timein, 'time_out'=> $timeout]);
$notification = array(
'message' => 'Employee Time Updated!',
'alert-type' => 'success'
);
return redirect()->back()->with($notification, 'Employee Time Updated!');
}