所选值从DB进入下拉选择框

时间:2019-06-19 08:00:15

标签: php laravel laravel-5

我有一个员工表格编辑。 我的问题是,我的编辑功能可以正常工作,但是我无法获取员工类型的值。

我有4个员工类型:

  • 工作人员
  • 主管
  • 经理
  • 超级用户

每次我去编辑表单时,下拉菜单中的员工类型始终在员工身上(我要显示,如果员工是“经理”,则下拉框显示“经理”)。

这是 editroledetails.blade.php

的代码
<div class="form-group">
    <label for="editCustomerType">Customer Type</label><br/>
    <select name="editCustomerType">
        <option value="Staff">Staff</option>
        <option value="Supervisor">Supervisor</option>
        <option value="Manager">Manager</option>
        <option value="Super User">Super User</option>
    </select>
</div>

控制器功能

public function editroledetails(Request $request)
{
    $user = \Auth::user();
    $userphone = 0;
    $reportTo = DB::select(DB::raw("select username from customer_type where customer_type = 'Supervisor' or customer_type ='Manager' "));

    $select = DB::select(DB::raw("select customer_type from customer_type "));
    $data = [
        'editUsername' => $request->editUsername,
        'editNik' => $request->editNik,
        'editEmail' => $request->editEmail,
        'editRegIdentities' => $request->editRegIdentities,
        'editReportTo' => $request->editReportTo,
        'editID' => $request->editID
    ];
    return view('editroledetails', compact('user', 'userphone', 'data', 'reportTo', 'select'));
}

3 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

<div class="form-group">
     <label for="editCustomerType">Customer Type</label><br/>
     <select name="editCustomerType">
           @if($user->customer_type == 'Staff')
              <option value="Staff">Staff</option>
           @endif
           @if($user->customer_type == 'Supervisor')
               <option value="Supervisor">Supervisor</option>
           @endif
           @if($user->customer_type == 'Manager')
               <option value="Manager">Manager</option>
            @endif
            @if($user->customer_type == 'Super User')
               <option value="Super User">Super User</option>
            @endif
     </select>
 </div>

答案 1 :(得分:0)

在选择选项上使用selected属性

<div class="form-group">
    <label for="editCustomerType">Customer Type</label><br/>
    <select name="editCustomerType">
        <option value="Staff" @if($user->customer_type == 'Staff')selected@endIf>Staff</option>
        <option value="Supervisor" @if($user->customer_type == 'Supervisor')selected@endIf>Supervisor</option>
        <option value="Manager" @if($user->customer_type == 'Manager')selected@endIf>Manager</option>
        <option value="Super User" @if($user->customer_type == 'Super User')selected@endIf>Super User</option>
    </select>
</div>

答案 2 :(得分:0)

您可以使用它在选择列表中显示所选项目

<?php
    $customerTypes = [
        'Staff' => 'Staff',
        'Supervisor' => 'Supervisor',
        'Manager' => 'Manager',
        'Super User' => 'Super User',
    ];
?>
<div class="form-group">
    <label for="editCustomerType">Customer Type</label><br/>
    <select name="editCustomerType">
        @foreach($customerTypes as $type)
            <?php
                $selected = "";
                @if($user->customer_type == $type) {
                    $selected = "selected";
                }
            ?>
            <option value="{{ $type }}" {{ $selected }} >{{ $type }}</option>
        @endforeach
    </select>
</div>