【问题标题】:Laravel 8 : Attempt to read property "user_id" on intLaravel 8:尝试在 int 上读取属性“user_id”
【发布时间】:2021-11-14 23:14:10
【问题描述】:

当我想创建我的门以授予编辑创建它的用户的事件的权限时,我遇到了这个错误。 谢谢你的帮助:)

我的列表页面:

@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;


        });
    }

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    allows 方法中删除Auth::id()

    if(! Gate::allows('Utilisateur', $event)) { 
        abort('403');
    }
    

    请注意,您不需要将当前经过身份验证的用户传递给这些方法。 Laravel 会自动将用户传递到门关闭。

    https://laravel.com/docs/8.x/authorization#authorizing-actions-via-gates

    【讨论】:

    • 非常感谢,它的作品:D
    猜你喜欢
    • 2021-04-28
    • 2021-06-25
    • 2021-05-25
    • 2022-06-10
    • 1970-01-01
    • 2021-08-08
    • 2022-12-13
    • 2022-11-16
    • 2021-05-22
    相关资源
    最近更新 更多