如何让我的自定义门在 Laravel 中工作?
How can I get my custom gate working in Laravel?
我正在尝试创建一个允许“管理员”团队的用户访问用户索引页面的自定义门。然而,它的功能与我想要实现的完全相反,我似乎不明白我哪里错了。
无论用户是否属于管理团队,它总是 returns“假”。
感谢您的帮助。谢谢。
User.php :
public function AdminTeam(string $team)
{
return null !== $this->teams()->where('name', $team)->first();
}
AuthServiceProvider.php :
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::define('is-admin', function ($user){
return $user->AdminTeam('Administrator');
});
}
UserController.php :
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if (Gate::allows('is-admin')) {
return view('admin.users.index', ['users' => User::paginate(10)]);
}
dd('you need to be admin!');
}
index.blade.php :
@can('is-admin')
@foreach($users as $user)
<tr>
<th scope="row">{{ $user->user_id }}</th>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
<td>
<a class="btn btn-sm btn-primary" href="{{ route('admin.users.edit', $user->user_id) }}"
role="button">Bearbeiten</a>
<button type="button" class="btn btn-sm btn-danger"
onclick="event.preventDefault();
document.getElementById('delete-user-form-{{ $user->user_id }}').submit()">
Löschen
</button>
<form id="delete-user-form-{{ $user->user_id }}"
action="{{ route('admin.users.destroy', $user->user_id) }}" method="POST"
style="display: none">
@csrf
@method("DELETE")
</form>
</td>
</tr>
@endforeach
@endcan
输出:
所以经过几天的调试,我意识到我的 teams()
方法应该在 User 模型中 team()
因为用户只有一个团队并且 BelongsTo 关系只会 return一条记录。
学分:https://www.reddit.com/r/laravel/comments/ldvtgd/custom_admin_gate_not_working/
https://laracasts.com/discuss/channels/laravel/admin-gate-not-working?page=1#reply=687840
更改如下:
User.php
public function team()
{
return $this->belongsTo(Team::class);
}
/**
* Check if the user belongs to Admin Team
* @param string $team
* @return bool
*/
public function isAdmin(string $team)
{
return $this->team()->where('name', $team)->exists();
}
AuthServiceProvider.php
public function boot()
{
$this->registerPolicies();
Gate::define('is-admin', function (User $user){
return $user->isAdmin('Admin');
});
}
UserController.php
public function index()
{
if (Gate::allows('is-admin')) {
return view('admin.users.index', ['users' => User::paginate(10)]);
}
dd('you need to be admin!');
}
我正在尝试创建一个允许“管理员”团队的用户访问用户索引页面的自定义门。然而,它的功能与我想要实现的完全相反,我似乎不明白我哪里错了。
无论用户是否属于管理团队,它总是 returns“假”。
感谢您的帮助。谢谢。
User.php :
public function AdminTeam(string $team)
{
return null !== $this->teams()->where('name', $team)->first();
}
AuthServiceProvider.php :
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::define('is-admin', function ($user){
return $user->AdminTeam('Administrator');
});
}
UserController.php :
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if (Gate::allows('is-admin')) {
return view('admin.users.index', ['users' => User::paginate(10)]);
}
dd('you need to be admin!');
}
index.blade.php :
@can('is-admin')
@foreach($users as $user)
<tr>
<th scope="row">{{ $user->user_id }}</th>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
<td>
<a class="btn btn-sm btn-primary" href="{{ route('admin.users.edit', $user->user_id) }}"
role="button">Bearbeiten</a>
<button type="button" class="btn btn-sm btn-danger"
onclick="event.preventDefault();
document.getElementById('delete-user-form-{{ $user->user_id }}').submit()">
Löschen
</button>
<form id="delete-user-form-{{ $user->user_id }}"
action="{{ route('admin.users.destroy', $user->user_id) }}" method="POST"
style="display: none">
@csrf
@method("DELETE")
</form>
</td>
</tr>
@endforeach
@endcan
输出:
所以经过几天的调试,我意识到我的 teams()
方法应该在 User 模型中 team()
因为用户只有一个团队并且 BelongsTo 关系只会 return一条记录。
学分:https://www.reddit.com/r/laravel/comments/ldvtgd/custom_admin_gate_not_working/ https://laracasts.com/discuss/channels/laravel/admin-gate-not-working?page=1#reply=687840
更改如下:
User.php
public function team()
{
return $this->belongsTo(Team::class);
}
/**
* Check if the user belongs to Admin Team
* @param string $team
* @return bool
*/
public function isAdmin(string $team)
{
return $this->team()->where('name', $team)->exists();
}
AuthServiceProvider.php
public function boot()
{
$this->registerPolicies();
Gate::define('is-admin', function (User $user){
return $user->isAdmin('Admin');
});
}
UserController.php
public function index()
{
if (Gate::allows('is-admin')) {
return view('admin.users.index', ['users' => User::paginate(10)]);
}
dd('you need to be admin!');
}