我有两个表,分别为machines
和calls
。 machines
表包含计算机及其详细信息的列表。当前,要记录有关机器的故障呼叫,管理员将输入machine_number
(以确保机器已在公司下注册)。此数字用于搜索数据库中的machines
表,结果将以表格形式显示。
我的问题是我希望遇到一种情况,即在提交表单时,在创建新行时,machines
表中计算机的详细信息将被更新(以防更改详细信息)。 calls
表中(以记录故障呼叫)。
以下是搜索功能:
Route::any('/search_results',function(){
$query = Input::get ( 'query' );
$machine= Machine::where('machine_id','LIKE','%'.$query.'%')->get();
return view('/addNewCall')->with('machine', $machine)->withQuery($query);
});
这是我的表格:
<form action="{{route('Call.store')}}" method="POST">
@csrf
@if(count($machine) > 0)
@foreach($machine as $machine)
<div class="col-md-6">
<div class="input-group" style="width: 100%;">
<label for="machine_id">{{ __('Machine ID') }}</label><br>
<input type="text" name="machine_id" id="machine_id" class="form-control" style="padding: 20px;" value="{{$machine->machine_id}}">
</div>
</div>
<div class="col-md-6" style="margin-top:20px">
<div class="input-group col-md-12" style="width: 100%;">
<label for="machine_custodian">{{ __('Machine Custodian Name') }}</label><br>
<input type="text" name="machine_custodian" id="machine_custodian" class="form-control" style="padding: 20px;" value="{{$machine->machine_custodian}}" required>
</div>
</div>
<div class="col-md-6" style="margin-top:20px">
<div class="input-group" style="width: 100%;">
<label for="call_logged_on">{{ __('Call logged on') }}</label><br>
<input type="datetime-local" name="call_logged_on" id="call_logged_on" class="form-control" style="padding: 20px;" required>
</div>
</div>
<div class="input-group col-xs-12 form-group" style="margin-top:20px">
<label for="fault_description">{{ __('Fault Description') }}</label><br>
<textarea name="fault_description" id="fault_description" class="form-control" rows="7" cols="50" required></textarea>
</div>
<div class="col-md-6">
<div class="input-group" style="width: 100%;">
<label for="assigned_FE">{{ __('Field engineer in charge') }}</label><br>
<input type="text" name="assigned_FE" id="assigned_FE" class="form-control" style="padding: 20px;" value="{{$machine->assigned_FE}}" required>
</div>
</div>
@endforeach
<button type="submit" class="btn-primary" style="padding: 10px; font-size: 16px; border: 0; margin-top:25px">{{ __('Log
Call') }}</button>
@else
<h2 style="margin:30px; font-style:italic; font-weight:lighter" class="text-center">Sorry, there is no ATM with the terminal id of "<b>{{ $query }}</b>" in our database</h2>
@endif
</form>
这是我的CallsController:
public function store(Request $request)
{
$this->validate($request,[
'machine_id' => 'required',
'machine_custodian' => 'required',
'call_logged_on' => 'required',
'fault_description' => 'required',
'assigned_FE' => 'required',
]);
//store in the database
$call = new Call;
$call->machine_id = $request->input('machine_id');
$call->machine_custodian = $request->input('machine_custodian');
$call->call_logged_on = $request->input('call_logged_on');
$call->fault_description = $request->input('fault_description');
$call->assigned_FE = $request->input('assigned_FE');
$call->save();
return redirect('/pages/newCall')->with('success', 'call created check pending calls to view call');
}
当前,代码在calls
表中创建一个新行。但是我不知道如何增强代码来更新machines
表中的机器信息。
注意:machine_id
列与id
表中的machines
列不同
答案 0 :(得分:1)
在return语句之前,您可以通过id
获取“机器”对象,并使用update()
方法更新所需的列:
$machine = Machine::find($call->machine_id);
$machine->update(['machine_custodian'=>$call->machine_custodian, 'assigned_FE'=>$call->assigned_FE]);
也可能是:
$machine = Machine::find($call->machine_id);
$machine->machine_custodian = $call->machine_custodian;
$machine->assigned_FE = $call->assigned_FE;
$machine->save();