Php Laravel-仅更新选定数据
我有一张桌子,在桌子里面,我有一个文本框,看看这个页面 现在的问题是,每当我更新特定的行或id时,它就会更新所有内容。如何在不更新所有内容的情况下更新特定行或id?多谢各位 这是我的控制器Php Laravel-仅更新选定数据,php,sql,laravel,laravel-5,eloquent,Php,Sql,Laravel,Laravel 5,Eloquent,我有一张桌子,在桌子里面,我有一个文本框,看看这个页面 现在的问题是,每当我更新特定的行或id时,它就会更新所有内容。如何在不更新所有内容的情况下更新特定行或id?多谢各位 这是我的控制器 public function updateSchedule(Request $request, $id) { $timein = $request->input('timeIn'); $timeout = $request->input('timeOut'); DB
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() !!}
{!!表单::打开(['action'=>['Admin\EmployeeFilemController@updateSchedule“,$employee->id],'method'=>'POST'])
员工编号和姓名:{{$Employee->Employee_No}:{{{$Employee->last_Name},{{{$Employee->first_Name}
您需要在查询中添加$id,这就是它更新整行的原因:
DB::table('schedules')->where('id', $id)->update(['time_in' => $timein, 'time_out' => $timeout]);
另外,我建议使用Eloquent来代替查询生成器。您可以使用where子句按id进行选择,例如
DB::table('schedules')->where('id', $id)
->update(['time_in' => $timein, 'time_out' => $timeout]);
此外,如果一切正常,您可以使用try a transaction,如果出现问题,则可以使用catch the error
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;
}
你也可以这样尝试
DB::update('update schedules set time_in = "'.$timein.'", time_out = "'.$timeout.'" where id = "'.$id.'");
您没有告诉数据库要更新哪一行。在编写查询时,“更新‘表’设置‘列名称’=‘值’”;但是你需要告诉它你想更新哪个id。比如'where'id'=12'。
希望您能理解。:)
您需要通过计划ID更新员工的计划时间,因为您必须更改多个计划timeIn
&timeOut
。更新你的代码如下。我希望提供的更改是不言自明的
刀片(仅限所需零件)
注意:即使未修改所有记录,此实现也会在时间上触及DB n(计划记录数)
提示:您可以将旧值与可修改值一起传递,以比较它们是否实际被修改,并仅对修改后的值执行更新查询
这里DB::table('schedules')->where('id',$id)->update(['time\u-in'=>$timein',time\u-out'=>$timeout])代码>它和你的一样sirCan你可以在哪里转储并显示你得到的信息吗?这个id是自动递增的吗?是的,先生,它是自动递增的,但没有任何错误。但问题是数据根本没有更新。但是它说这是一个成功,虽然它没有更新眼睛,但在您的情况下,它将始终提供成功,因为您没有检查查询结果,也没有使用成功消息进行简单的重定向。让我们看看。是的,您是正确的。我的意思是没有我想要更新的特定id,我只是想在我更改输入文本值时更新id,因为现在代码不起作用,先生:(我仍然不清楚您是否说“我只想在我更改输入文本值时更新id”。据我所知,我认为id主键正确吗?如果您添加($id),您会得到什么在您的控制器中?这段代码非常符合逻辑,谢谢您,先生,但它仍然不起作用:(当我将$id更改为2时,它正在更新,但当我将其更改为$id时,它不会更新:(帮助先生,谢谢dd($schedules)
)的输出是什么?dd($schedules)
数组:1[▼ “$setTime->id”=>数组:2[▶] ]对不起我的不好,更新了答案。
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!');
}
<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>
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!');
}