Support the ongoing development of Laravel.io →
Authentication Security Validation

I am trying to move the controls I make in the Controller for a User to be able to leave a Comment on another User's Post. The code worked in the Controller but now as I moved it in a Policy I always get a 403 error (Forbidden). This is the policy, it is registered in AuthServiceProvider like :

Comment::class => CommentPolicy::class,

The Policy:

class CommentPolicy
{
    
    public function leaveComment(User $user, Post $post)
    {

        $following = Follower::where('follower_id', $user->id)->where('publisher_id', $post->user_id)->select('enable_follow')->get();

        if (!$following->isEmpty()) {

            $enabled = $following[0]['enable_follow'];

            if ($enabled != 0) {

                return true;

            } else {

                return false;

            }
        } else if ($following->isEmpty()) {

            return true;

        }

    }

}

I for the life of me can't understand why in heaven this is not working. In my controller I am passing the $post as

$post = Post::findOrFail($post_id);

$this->authorize('leaveComment', $post);

Does anybody have a clue?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

chriz74x chriz74x Joined 8 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.