sork

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

Posted 7 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.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.