我有以下与Form Collective软件包一起使用的代码,但是由于该软件包尚未针对5.5进行更新,因此该代码目前无法正常工作。我也在使用Spatie的Laravel许可包
我的代码是
@foreach ($permissions as $permission)
{{Form::checkbox('permissions[]', $permission->id, $role->permissions ) }}
{{Form::label($permission->name, ucfirst($permission->name)) }}<br>
@endforeach
我相信这只是循环遍历permissions
,如果permission
属于当前role
,请选中该框。
不使用包装怎么办?
我目前已经尝试过
@foreach ($permissions as $permission)
<div class="checkbox">
<label>
{{ ucfirst($permission->name) }}
</label>
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}">
<br>
</div>
@endforeach
但是我不确定如何根据角色在列表中是否具有权限来附加选中的属性。
参考方案
只需将checked
属性添加到复选框HTML:
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" checked>
如果需要根据条件进行设置,请使用以下代码:
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" @if(/* some condition */) checked @endif>
编辑
由于我之前不理解这个问题,因此我将添加一些细节。
假设您的Role
模型具有附加权限的集合,并将其存储在属性$role->permissions
中,则可以执行以下操作
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" @if($role->permissions->contains($permission)) checked @endif>
这样,您可以检查您的角色是否具有ID为$permission->id
的权限。
我正在编写一些Laravel Blade模板,并且我的模型可能包含空对象。我非常想尝试获取对象属性,如果有错误,则返回null。因此,不必编写以下代码:@if ($model->child_object_that_may_be_null) {{ $model->child_object_that_may_be_null->interesti…
检查对象是否已在集合中-Laravel - php当我循环一系列不同的结果时,我希望将对象添加到新集合中。查询:$osRed = Item::where('category', 'Hardware') ->where(function ($query) { $query->where('operating_system', '…
Laravel-无需登录即可认证用户 - php在laravel 5.4中,可以在不登录用户的情况下对用户进行身份验证吗?从laravel doc中,我能找到的最接近的东西是:Auth::once($credentials) 但这仍然使该用户感到厌烦。我需要做的就是只是知道使用该电子邮件和密码的用户是否存在。 参考方案 您可以将Auth::attempt函数与第三个参数用作false进行登录 $email…
Laravel 5错误报告抑制 - php在Laravel 4中,抑制E_NOTICE消息很容易;我似乎无法做到这一点,因为如果我添加error_reporting(E_ALL ^ E_NOTICE) 它只是被覆盖的任何地方。这似乎发生在这里:(index.php)$response = $kernel->handle( $request = Illuminate\Http\Request::…
Laravel-foreach局部变量正在覆盖全局变量 - php今天,我面临一个非常奇怪的问题。我有一个控制器,将从该控制器向视图sections thread和threads发送三个变量。在我看来,我正在使用foreach循环遍历所有部分,如下所示:@foreach($sections as $i => $section) 对于每个部分,我都将创建一个div并给出如下的ID:id="{{ $thread…