i'd suggest to inject your team it to your controller via route , it's just cleaner since you have to read the object from db laravel will do this for you just pass the id in your route and catch it in controller argument , and yes i'd put it in team policy since the condition is in the team entity not the user or tasklist
/tasklists/store/{team}
function store(Request $request , Team $team){
Gate::authorize('isMember', $team);
return $team->tasklists()->create( validated data );
}
you dont have to pass the authenticated user to policy , it will be injected to policy method automatically
public function isMember(User $user, Team $team)
{
return $user->teams->contains($team->id)
}
if you want to have specific message for failed policy you can put it in your policy instead of controller , you might want to use it in multiple controllers why add a new line to all of them
public function isMember(User $user, Team $team): bool|Response
{
if(! $user->teams->contains($team->id))
{
return Response::deny('you are not a team member');
}
return true ;
}
if you need to pass extra argument to policy you have to pass them as array with the model inside that array
Gate::authorize('isMember' , [ $team , $extraArgument ] );
but generally the first argument you pass to policy (after action/ability ) will determine which policy should be executed
Also i 'd recommand to flag the model when you creating policy with artisan if you are a beginner
php artisan make:policy TeamPolicy --model=Team