如何在laravel的下拉列表中获取记录?

问题描述 投票:0回答:2

我在项目中编辑了个人资料页面。

在我的编辑页面文件中

@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" : "[email protected]"
    "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
    },
database laravel mongodb edit
2个回答
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>

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>
© www.soinside.com 2019 - 2024. All rights reserved.