如何在Laravel中的编辑视图中获取记录drom数据库? [英] How can I fetch record drom database in edit view In Laravel?
问题描述
在我的编辑视图中,我有这样的代码
In my edit view I have code like this
<div class="form-group">
<label class="col-md-12">Last Name</label>
<div class="col-md-12">
<input type="text" placeholder="Enter Last Name" name="lastName" class="form-control form-control-line" value="{{$profile->personal_detail['last_name']}}" required>
</div>
</div>
<div class="form-group">
<label class="col-md-12">Department</label>
<div class="col-md-12">
<select class="custom-select form-control col-md-11" id="department" name="department">{{ $profile->personal_profile['department'] }}
@foreach($listDepartment as $departmentList){
<option value='{{$departmentList->nameOfDepartment}}'>{{$departmentList->nameOfDepartment}}</option>
}
@endforeach
</select>
</div>
</div>
在编辑"视图的我的姓氏"字段中,它为我提供了数据库的姓氏,在部门"中,它显示我部门的下拉列表,但我希望在该字段中插入部门的名称.
In edit view my last name field it gives me last name from database, in department it display me drop down of department but I want that inserted name of department in that field.
我怎么得到它?
我还有其他这样的下拉列表
I have other drop down like this
<div class="row">
<label class="col-md-6"><b> Mode </b></label>
<div class="col-md-6">
<select class="custom-select form-control col-md-12" name="mode" id="mode" required>
<option value=""> --- Select Interciew Mode --- </option>
<option value="telephonic">Telephonic</option>
<option value="facetoface">Face 2 face</option>
<option value="skype">Skype</option>
</select>
</div>
</div><hr>
这是我的控制器
public function candidateDetail($id)
{
$empDetails = User::all();
$candidateDetail = EmployeeHire::find($id);
$interview = [
'' => '--- Select Interciew Mode ---',
'telephonic' => 'Telephonic',
'facetoface' => 'Face 2 face',
'skype' => 'Skype'
];
return view('pages.candidatedetails', compact('id', 'candidateDetail', 'empDetails', 'interview'));
}
推荐答案
您可以在foreach
中检查nameOfDepartment
的值是否与用户的值相匹配.
You can check in your foreach
if the value of nameOfDepartment
match the one from your user.
<div class="form-group">
<label class="col-md-12">Department</label>
<div class="col-md-12">
<select class="custom-select form-control col-md-11" id="department" name="department">
@foreach($listDepartment as $departmentList)
@if ($profile->personal_profile['department'] == $departmentList->nameOfDepartment)
<option value="{{$departmentList->nameOfDepartment}}" selected="selected">{{$departmentList->nameOfDepartment}}</option>
@else
<option value="{{$departmentList->nameOfDepartment}}">{{$departmentList->nameOfDepartment}}</option>
@endif
@endforeach
</select>
</div>
</div>
对于第二个选择字段,请在控制器中创建一个包含所有可能值的数组.
For your second select field, create an array with all possible values in your controller.
$interview = [
'' => '--- Select Interciew Mode ---',
'telephonic' => 'Telephonic',
'facetoface' => 'Face 2 face',
'skype' => 'Skype'
];
然后,您可以执行与先前选择相同的操作:
Then you can do the same as your previous select :
<select class="custom-select form-control col-md-12" name="mode" id="mode" required>
@foreach($interview as $key => $name)
@if ($profile->personal_profile['interview'] == $key)
<option value="{{ $key }}" selected="selected">{{ $name }}</option>
@else
<option value="{{ $key }}">{{ $name }}</option>
@endif
@endforeach
</select>
这篇关于如何在Laravel中的编辑视图中获取记录drom数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!