8 months ago

Policy Not working. Always return False

Posted 8 months ago by hamza

I am new to using Policies in Laravel. I am using Policy for the PostController so that only the user with the appropriate permission can edit and update the post. But it always returns false.

Here is PostController edit method

public function edit(Post $post)
    $this->authorize('edit', $post);

    $post = $post->load(['categories', 'user']);

    $categories = Category::all();

    return view('post.edit', compact('post', 'categories'));

here is my Post Policy Class edit method

public function edit(User $user, Post $post)
    $role = $user->role->name;

    if ($role == 'Admin' || $role == 'Editor') {
        return true;

    return $user->id == $post->user_id;

I tried to return true in the Edit Method of the PostPolicy but it still do not work.

I have added the following to the AuthServiceProvider

protected $policies = [
    'Vanguard\Model' => 'Vanguard\Policies\ModelPolicy',
    Post::class => PostPolicy::class,

Also Auth middleware is already added. Please I need urgent help

Please sign in or create an account to participate in this conversation.