why is my Gate always denied the request on laravel

263 views Asked by At

Little help here. I have two models user and post. So i want to restrict a user from editing post's of other users so i used this:

public function boot()
    {
        $this->registerPolicies();

        //
        Gate::define('update-post', function (User $user, Post $post) {
              return $user->id === $post->user_id;
        });
    }

In App\Providers\AuthServiceProvider.php. and in my controller at edit method i use this code:

public function edit($id, Post $post, User $user)
    {
       
       if(Gate::allows('update-post', $post)){
          $posts = Post::find($id);
          return view('pages.edit')->with('posts', $posts);
          
       }
       
        return redirect('/')->with('message', 'you cannot edit this post');
    }

It works but when i clicked on edit button from the two users account it goes to what i redirdect on the edit controller.

So how can i fix that, please help!!!

0

There are 0 answers