Laravel 8 : 尝试在 int 上读取 属性 "user_id"
Laravel 8 : Attempt to read property "user_id" on int
当我想创建我的门以授予创建它的用户编辑事件的权限时,我遇到了这个错误。
感谢您的帮助:)
我的列表页面:
@if(Gate::allows('Utilisateur', $events))
<a class="btn btn-warning" href="{{route('events.edit',$events->id)}}">Editer</a>
@endif
事件控制器:
public function edit($id)
{
if(!Auth::check())
{
return redirect('login');
}
$event=Events::findOrFail($id);
if(!Gate::allows('Utilisateur', Auth::id(), $event)){
abort('403');
}
return view('events.edit', ['events' => Events::findOrFail($id)]);
}
授权服务提供商:
public function boot()
{
$this->registerPolicies();
Gate::define('Utilisateur', function ($user,$event) {
// dd("zzzz".$event);
if($user->id===$event->user_id){
return 1;
}
return 0;
});
}
从 allows
方法中删除 Auth::id()
。
if(! Gate::allows('Utilisateur', $event)) {
abort('403');
}
Note that you are not required to pass the currently authenticated user to these methods. Laravel will automatically take care of passing the user into the gate closure.
https://laravel.com/docs/8.x/authorization#authorizing-actions-via-gates
当我想创建我的门以授予创建它的用户编辑事件的权限时,我遇到了这个错误。 感谢您的帮助:)
我的列表页面:
@if(Gate::allows('Utilisateur', $events))
<a class="btn btn-warning" href="{{route('events.edit',$events->id)}}">Editer</a>
@endif
事件控制器:
public function edit($id)
{
if(!Auth::check())
{
return redirect('login');
}
$event=Events::findOrFail($id);
if(!Gate::allows('Utilisateur', Auth::id(), $event)){
abort('403');
}
return view('events.edit', ['events' => Events::findOrFail($id)]);
}
授权服务提供商:
public function boot()
{
$this->registerPolicies();
Gate::define('Utilisateur', function ($user,$event) {
// dd("zzzz".$event);
if($user->id===$event->user_id){
return 1;
}
return 0;
});
}
从 allows
方法中删除 Auth::id()
。
if(! Gate::allows('Utilisateur', $event)) {
abort('403');
}
Note that you are not required to pass the currently authenticated user to these methods. Laravel will automatically take care of passing the user into the gate closure.
https://laravel.com/docs/8.x/authorization#authorizing-actions-via-gates