简体   繁体   中英

drop-down selected option old value editing is in not working in laravel

I am trying to show the selected option value of drop-down and just used blew code and that's now working even I used the same trick for another table data and that is working fine. But I don't know why it's not working?

public function userEdit($id) {

    $user = User::find($id);
    $userType = User::first()->userType;
    $isActive = User::first()->isActive;
    $allRoles=Role::all();
    $role_user = $user->roles()->pluck('user_id','role_id')->toArray();

    return view('admin\userEdit',compact('user','allRoles','userType','isActive','role_user','selectedRole'));


}

<div class="form-group">
<label class="control-label col-md-3">User Type
    <span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-6">
    <div class="input-icon right">
        <i class="fa"></i>
        <select class="form-control"
            name="userType">

            <option @if(old('userType',$userType) == 'host') selected @endif>
                host
            </option>
            <option @if(old('userType',$userType) == 'visitor') selected @endif>
                visitor
            </option>
            <option @if(old('userType',$userType) == 'admin') selected @endif>
                admin
            </option>
            <option @if(old('userType',$userType) == 'operator') selected @endif>
                operator
            </option>

        </select>
    </div>
</div>

Can anyone help me to fix this that will show my old value when user will edit the data?

i used to have the same question and i had to do it the unprofessional way, we all know select is not restricted for repeating options in it so i used that hack. For your case i would do something like :

<select class="form-control"
    name="userType">
    <option value"{{ $userType }}"> {{ $userType }} </option>
    <option value"host"> host </option>
    <option value"admin"> admin </option>
    <option value"visitor"> visitor </option>
    <option value"operator"> operator </option>
 </select>

For your variables you are sending to the blade file can you try this code to make sure you dont get the same userType, isActive and all

$user = User::find($id);
$userType = $user->userType;
$isActive = $user->isActive;
$allRoles=Role::all();
$role_user = $user->roles()->pluck('user_id','role_id')->toArray();

I am so confident this is not the best way to go about it but it solves your problem currently.

Even if $userType be host , it will give you host when posting the form

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