简体   繁体   中英

Selected value get from DB into dropdown select box

I have an Employee Form Edit. My problem is, my function for the edit is working fine but I can't get the value of my employee type.

I have 4 employee type:

  • Staff
  • Supervisor
  • Manager
  • Super User

Every time I go to edit form, the employee type on drop down is always on staff (I want to display, if the employee is "Manager" then the drop box show Manager).

This is the code for 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>

Controller function

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'));
}

You can do something like this:

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

Use the selected attribute on the select options

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

You can use this to show the selected item in select list

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM