sork
113
4
Laravel

$this->authorize() does not work in controller

Posted 11 months ago by sork

I have created OfferPolicy class to provide authorization to resource action in Offer model. i have followed below steps:

create OfferPolicy.

php artisan make:policy OfferPolicy --model=Offer

register OfferPolicy in AuthServiceProvider

protected $policies = [
        Offer::class => OfferPolicy::class        
    ];

update policy in OfferPolicy class


public function update(User $user, Offer $offer)
    {
        return $user->id == $offer->user_id;
    }

OfferController


public function update(Request $request, Offer $offer)
    {
        $this->authorize('update', $offer);
        
        $offer->update($request->all());

    return redirect()->route('offers.index')
}

when I am trying to update an offer, below error is coming.

Symfony \ Component \ HttpKernel \ Exception \ AccessDeniedHttpException
This action is unauthorized.

Anyone can help me on this problem, did i forget anything?

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

Reply to

Use Markdown with GitHub-flavored code blocks.