如何在Laravel中的编辑视图中获取记录drom数据库?

时间:2019-05-03 07:12:22

标签: php laravel mongodb eloquent

在我的编辑视图中,我有这样的代码

<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>

在“编辑”视图的“我的姓氏”字段中,它为我提供了数据库的姓氏,在“部门”中,它显示我部门的下拉列表,但我希望在该字段中插入部门的名称。

我怎么得到它?

我还有其他这样的下拉列表

<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'));
    }

1 个答案:

答案 0 :(得分:1)

您可以检查foreach的值是否与用户的nameOfDepartment相匹配。

<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>

对于第二个选择字段,请在控制器中创建一个包含所有可能值的数组。

$interview = [
    '' => '--- Select Interciew Mode ---',
    'telephonic' => 'Telephonic',
    'facetoface' => 'Face 2 face',
    'skype' => 'Skype'
];

然后,您可以执行与先前选择相同的操作:

<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>