我在项目中有编辑个人资料页面。
在我的编辑页面文件中
@foreach($userProfile as $profile)
@if($userId == $profile->id)
<div class="form-group">
<label class="col-md-12">First Name</label>
<div class="col-md-12">
<input type="text" placeholder="Enter First Name" name="firstName" class="form-control form-control-line" value="{{$profile->personal_detail['first_name']}}" required>
</div>
</div>
<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)
{
<option value='{{$departmentList->nameOfDepartment}}'>{{$departmentList->nameOfDepartment}}</option>
}
@endforeach
</select>
</div>
@endif
@endforeach
请参见名字,我在value =“”标签中编写代码,以便它从数据库中获取名字并显示给我。我如何在下拉菜单中做同样的事情?
在我的数据库集合名称中是用户
"role_id" : "5c8a51ed650fbd5398503044",
"username" : "Neel",
"company_email" : "asd@abc.com"
"personal_detail" : {
"emp_id" : "101",
"first_name" : "Abc",
"middle_name" : "D",
"last_name" : "Efg",
"gender" : "Male",
"city" : "USA",
"state" : "HY",
"department" : "Laravel",
"designation" : "Junior Laravel Developer",
"total_experience" : null,
"about_me" : null
},
答案 0 :(得分:0)
首先,我建议在选项中使用记录ID作为值。
一种简单(但不一定是最好的)方法是使用if
检查当前值。使用当前代码是这样的:
<option value='{{$departmentList->nameOfDepartment}}' @if($profile->nameOfDepartment == $departmentList->nameOfDepartment) selected @endif>{{$departmentList->nameOfDepartment}}</option>
具有ID(假设每个配置文件记录中都有一个department_id
)
<option value='{{$departmentList->id }}' @if($profile->department_id == $departmentList->id) selected @endif>{{$departmentList->nameOfDepartment}}</option>
答案 1 :(得分:0)
请尝试使用此代码
<select name="" id="" class="form-control">
<option value="" disabled="disabled" selected="selected">Select</option>
@foreach($listDepartment as $departmentList)
<option {{ isset($departmentList->id) && $departmentList->id == $profile->id ? 'selected="selected"':'' }} value="{{$departmentList->id}}">{{$departmentList->nameOfDepartment}}
</option>
@endforeach
</select>