0

I am working on a Laravel 10 on Visual Studio Code with intelephense extension installed. I am getting error on my policy classes saying,

"Expected type 'bool'. Found 'Illuminate\Auth\Access\Response'.intelephense(1006)"

Here is a portion of my code:

public function create(User $user): bool
{
    $roles = $user->roles->pluck('id')->toArray();
    return in_array(Role::TRAINING_MANAGER, $roles)
        ? Response::allow()
        : Response::deny();
}

How to get ride of this error notification?

Update

I should have changed the return type to Response.

Ayenew Yihune
  • 1,059
  • 13
  • 19
  • 1
    why are you setting the response to bool? it clearly says it returns a Response instance. https://github.com/laravel/framework/blob/10.x/src/Illuminate/Auth/Access/Response.php#L52 – N69S Mar 02 '23 at 09:52
  • Laravel 10 provides default return types, and that is the default value. – Ayenew Yihune Mar 02 '23 at 10:57
  • 1
    Laravel policies has a HandlesAuthorization trait which handles the deny and allow for you, all what you need is to return true or false, check @Top-Master answer and choose the the solution you want – Znar Mar 02 '23 at 12:19
  • @N69S Sure, I should have changed the return type to Response. – Ayenew Yihune Mar 02 '23 at 12:28

1 Answers1

4

If OP's create(...) is a route-handler (aka controller):

Simple, remove : bool of the controller/handler.

The line:

public function create(User $user): bool

Should be:

public function create(User $user)

Else, try:

public function create(User $user): bool
{
    $roles = $user->roles->pluck('id')->toArray();
    return in_array(Role::TRAINING_MANAGER, $roles);
}
Top-Master
  • 7,611
  • 5
  • 39
  • 71