【问题标题】:How can I fetch record in dropdown in laravel?如何在 laravel 的下拉列表中获取记录?
【发布时间】:2019-09-05 01:40:16
【问题描述】:

我的项目中有编辑个人资料页面。

在我的编辑页面文件中

@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

请参阅 In first name 我在 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
    },

【问题讨论】:

  • 您已经在部门下拉列表中完成了。你到底想做什么?
  • 在部门我的第一条记录是 laravel,而在我员工的部门是 HR,所以我想获取 HR 而不是 laravel
  • 表之间有关系吗? @John_rees
  • 我正在使用 mongoDb,但在我的用户表中部门字段已经存在
  • 能否请您展示您的数据库结构。从您要获取记录的位置

标签: database laravel mongodb edit


【解决方案1】:

请试试这个代码

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

【讨论】:

  • 其实可以,但是无法从数据库中获取记录
【解决方案2】:

首先,我建议在选项中使用记录 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>

【讨论】:

  • 不,这不是工作人员,在此记录中,我在 HR 中的那个员工的部门和我的部门集合的第一条记录是 Laravel 所以它显示我 laravel,而不是 laravel 我想获取 HR 因为,在添加员工表格我在部门中插入了 HR
猜你喜欢
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 2015-05-22
  • 1970-01-01
  • 2020-01-31
  • 2018-08-05
  • 2018-10-05
相关资源
最近更新 更多