选择选项,选择的值返回重复项

时间:2019-04-25 19:05:08

标签: laravel laravel-5

我正在尝试获取旧的选定值并将其显示在选择选项以及非选定值内,但是,当我尝试进行比较时,它似乎不起作用,到目前为止,我只能得到重复项。实施它的最佳方法是什么?

这就是我的代码的样子。

控制器:

public function index()
{
    $roles = Role::get();
    $permissions = Permission::get();

    return view('role.index', compact(['roles','permissions']));

}

角色视图:

@foreach($role->permission as $permissioninrole)
    <option name="permissions[]" {{ old('name', $permissioninrole->name) == $permissioninrole->name ? 'selected' : '' }} value="{{$permissioninrole->id}}">{{$permissioninrole->name}}</option>
@endforeach

@isset($permissioninrole)
    @foreach($permissions as $permission)
        @if($permissioninrole->name != $permission->name)
            <option class="rem" name="permissions[]" value="{{$permission->id}}">{{$permission->name}}</option>
        @endif
    @endforeach
@endisset

@empty($permissioninrole)
    @foreach($permissions as $permission)
        <option name="permissions[]" value="{{$permission->id}}">{{$permission->name}}</option>
    @endforeach
@endempty

外观。 HTML

<option name="permissions[]" selected value="15">role-create</option>
<option name="permissions[]" selected value="16">role-read</option>
<option name="permissions[]" value="15">role-create</option>
<option name="permissions[]" value="16">role-read</option>
...

如果角色“ x”具有角色创建权限,应该怎么做。

<option name="permissions[]" selected value="15">role-create</option>
<option name="permissions[]" value="16">role-read</option>
...

1 个答案:

答案 0 :(得分:0)

解决方案

for element in self