I have used the generator to generate a simple policy:
php artisan make:policy TeamPolicy
And, I have registered it in AuthServiceProvider
as:
protected $policies = [
Team::class => TeamPolicy::class,
];
I tried to call it in the TeamsController
as:
$this->authorize('update', $team);
Here is my policy file Policies\TeamPolicy.php
as:
<?php
namespace App\Policies;
use App\Team;
use Illuminate\Auth\Access\HandlesAuthorization;
class TeamPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
//
}
public function update( Team $team)
{
App:debug("Policy update called!");
return true;
}
}
However, the update method in the policy is never called, and I get error 403 when calling $this->authorize('update', $team);
Please advise!
The first argument of the Policy methods should be the user to check authorization on. Try instead:
public function update(User $user, Team $team)
{
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With