显示从 Laravel 数据库中选中的复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46549649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:44:56  来源:igfitidea点击:

Show checkbox checked from database in laravel

phplaravellaravel-5.4blade

提问by Pawan Rai

I'm using Laratrust package for ACL in my application. I'm trying to edit roles (assign permissions to role) using checkbox. And want get already assigned permission checkbox state checked from database.

我在我的应用程序中将 Laratrust 包用于 ACL。我正在尝试使用复选框编辑角色(为角色分配权限)。并希望从数据库中检查已分配的权限复选框状态。

Code in RoleController.php

输入代码 RoleController.php

    public function edit($id)
{
  $role = Role::where('id', $id)->with('permissions')->first();
  $permissions = Permission::all();
  return view('admin.manage.roles.edit')->withRole($role)->withPermissions($permissions);
}

Below is the code what I have tried:

以下是我尝试过的代码:

@foreach($permissions as $permission)
 <div class="checkbox checkbox-styled">
   <label>
    <input type="checkbox" name="permissions[]" value="{{$permission->id}}" 
    {{ $role->permissions->pluck('id') == $permission->id ? 'checked' : '' }}
    >
    <span>{{$permission->display_name}}  <em>({{$permission->description}})</em></span>
</label>
</div>
@endforeach

The code is throwing error Object of class Illuminate\Support\Collection could not be converted to int

代码抛出错误 Object of class Illuminate\Support\Collection could not be converted to int

I had tried:

我试过:

{{ $role->permissions->id == $permission->id ? 'checked' : '' }}

{{ $role->permissions->id == $permission->id ? 'checked' : '' }}

This throws error:

这会引发错误:

Property [id] does not exist on this collection instance

Property [id] does not exist on this collection instance

When I do {{dd($role->permissions)}}: The following output was given:

当我这样做时{{dd($role->permissions)}}:给出了以下输出:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

I would be very thankful if anyone could point-out mistake I'm doing here.

如果有人能指出我在这里做的错误,我将不胜感激。

回答by Vikash

Your code will not work because you are trying to compare array with a string, which is impossible. you can use php in_arrayfunction to check whether your permission exist for the current permission or not

您的代码将不起作用,因为您试图将数组与字符串进行比较,这是不可能的。您可以使用phpin_array函数检查当前权限是否存在您的权限

I think you are trying to check all the permission which already exist for the specific roles. correct me if i am wrong.

我认为您正在尝试检查特定角色已经存在的所有权限。如果我错了,请纠正我。

Try this

尝试这个

<input type="checkbox" name="permissions[]" value="{{$permission->id}}" 
@if($role->permissions) @if(in_array($permission->id, $role->permissions->pluck('id')) checked @endif @endif>

Hope this will help :)

希望这会有所帮助:)

回答by tarikhs

Easiest way to do is to check in collection using contain

最简单的方法是使用 contains 检入集合

@if($role->permissions->contains($permission->id)) checked=checked @endif

@if($role->permissions->contains($permission->id)) checked=checked @endif

<input type="checkbox" name="permissions[]" value="{{$permission->id}}" 
{{ @if($role->permissions->contains($permission->id)) checked=checked @endif }}
>

回答by Victor Bala

Another short method.

另一个简短的方法。

<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
{{ $role->permissions->contains($permission->id) ? 'checked' : '' }}>