简体   繁体   English

下拉选择的选项旧值编辑在Laravel中不起作用

[英]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. 我曾经有过同样的问题,我不得不以非专业的方式来做,我们都知道select并不受重复选项的限制,所以我使用了这种技巧。 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 对于要发送到刀片文件的变量,可以尝试以下代码以确保未获得相同的userType,isActive和所有

$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 即使$userTypehost ,它也会在发布表单时为您提供host

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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