温馨提示:本文翻译自stackoverflow.com,查看原文请点击:laravel - How do create Hierarchical dropdownlist for Edit View Blade

laravel - 如何为Edit View Blade创建分层下拉列表

发布于 2020-03-28 23:31:41

我正在使用Laravel-5.8开发Web应用程序。

我已经完成了create.blade,如下所示:

public function create()
{
    $supervisors = Employee::all();
    return view('employees.create')->with('supervisors', $supervisor);
}

<select class="form-control select2bs4" data-placeholder="Choose Supervisor" tabindex="1" name="supervisor_id" style="width: 100%;">>
    <option value="">Select Supervisor</option>
     @if($supervisors->count() > 0)
        @foreach($supervisors as $supervisor)
           <option value="{{$supervisor->id}}">{{$supervisor->employee_code}}</option>
        @endforeach
     @endif
</select>

我有这个Laravel模型:

class Employee extends Model
{
    protected $table = 'employees';
    protected $fillable = [
              'employee_code',
              'supervisor_id',
              'first_name',
              'emp_image',
              'last_name',
          ];

    public function supervisor()
    {
        return $this->belongsTo('App\Models\Employee','supervisor_id');
    }
}

edit.blade

<div class="col-12 col-sm-4">
   <div class="form-group">
     <label class="control-label"> Supervisor:</label>
      <select class="form-control select2bs4" data-placeholder="Choose Employee Type" tabindex="1" name="supervisor_id" style="width: 100%;">
       <option value="">Select Supervisor</option>
         @if($supervisors->count() > 0 )
          @foreach($supervisors as $supervisor)
            <option value="{{$supervisor->id}}" @if($supervisor->id == $employee->id) selected @endif>{{$supervisor->employee_code}}</option>
          @endforeach
         @endif
     </select>
   </div>
 </div>

这给我的下拉值与加载的员工相同。

如何像创建视图刀片一样为主管下拉列表编写编辑视图刀片?

谢谢。

查看更多

查看更多

提问者
mikefolu
被浏览
15
zahid hasan emon 2020-01-31 18:06

您在控制器中的编辑功能

public function edit($id)
{
    $employee = Employee::findOrFail($id);
    $supervisors = Employee::all();
    return view('employees.edit')->with('supervisors', $supervisor);
}

和编辑页面的代码

<select class="form-control select2bs4" data-placeholder="Choose Supervisor" tabindex="1" name="supervisor_id" style="width: 100%;">>
    <option value="">Select Supervisor</option>
     @if($supervisors->count() > 0)
        @foreach($supervisors as $supervisor)
           <option value="{{$supervisor->id}}" @if( $employee->supervisor_id == $supervisor->id )
                    selected
                @endif>{{$supervisor->employee_code}}</option>
        @endforeach
     @endif
</select>

发布
问题

分享
好友

手机
浏览

扫码手机浏览