如何在 Laravel 8 中创建一个带参数的 Blade 指令?
How can I make a Blade directive that takes a parameter, in Laravel 8?
我正在开发 Laravel 8 应用程序。
我需要创建一个自定义指令来检查用户的权限:
所以,在 app\Providers\AppServiceProvider.php
,我做了:
public function boot() {
Schema::defaultStringLength(191);
Blade::directive('hasPermissionTo', function ($permission) {
return in_array($permission, $this->user_permissions);
});
}
问题在于,在 Blade 内部,hasPermissionTo
必须与参数一起使用:
@hasPermissionTo('view-users')
<h2>Users</h2>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
@if ($users)
<tbody>
@foreach ($users as $user)
<tr>
<td>{{ $user->first_name }} {{ $user->last_name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
@endif
</table>
@endhasPermissionTo
我该如何解决这个问题?
Blade::if('hasPermissionTo', function ($permissionToBeChecked) {
return in_array($permissionToBeChecked, $this->user_permissions);
});
像你一样使用。
我正在开发 Laravel 8 应用程序。
我需要创建一个自定义指令来检查用户的权限:
所以,在 app\Providers\AppServiceProvider.php
,我做了:
public function boot() {
Schema::defaultStringLength(191);
Blade::directive('hasPermissionTo', function ($permission) {
return in_array($permission, $this->user_permissions);
});
}
问题在于,在 Blade 内部,hasPermissionTo
必须与参数一起使用:
@hasPermissionTo('view-users')
<h2>Users</h2>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
@if ($users)
<tbody>
@foreach ($users as $user)
<tr>
<td>{{ $user->first_name }} {{ $user->last_name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
@endif
</table>
@endhasPermissionTo
我该如何解决这个问题?
Blade::if('hasPermissionTo', function ($permissionToBeChecked) {
return in_array($permissionToBeChecked, $this->user_permissions);
});
像你一样使用。