spatie/laravel-permission

permission:show command fails with preventAccessingMissingAttributes enabled

Cbrad24 opened this issue ยท 6 comments

Describe the bug
Laravel 9.35 introduced Eloquent Strictness parameters. Unfortunately, with strictness enabled, when running php artisan permission:show it throws an error due to the command trying to access the team_id property on the Role model.

Screenshot 2023-06-30 164754

Versions

  • spatie/laravel-permission package version: 5.10.1
  • illuminate/framework package: v10.14.1

PHP version: 8.2.7

Database version: Postgres 15

To Reproduce
Steps to reproduce the behaviour:

  1. Spin up a Laravel app using laravel/framework >= 9.35.
  2. Place Model::preventAccessingMissingAttributes(); in AppServiceProvider::boot() method.
  3. Install Laravel Permission package.
  4. Ensure teams support is disabled.
  5. Run migrations.
  6. Run php artisan permission:show.

Expected behaviour
The command should run however it throws an error.

Additional context
Obviously as a workaround you can disable Eloquent Strictness to get the command to run, however this is not a great forever solution.

Environment (please complete the following information, because it helps us investigate better):

  • OS: WSL Ubuntu
  • Version: 20.04
drbyte commented

@erikn69 Have you run into this?

I believe it is due to $team_key => $role->$team_key in the end of the mapping array on line 41:

$roles = $roleClass::whereGuardName($guard)
->with('permissions')
->when(config('permission.teams'), fn ($q) => $q->orderBy($team_key))
->orderBy('name')->get()->mapWithKeys(fn ($role) => [
$role->name.'_'.($role->$team_key ?: '') => ['permissions' => $role->permissions->pluck('id'), $team_key => $role->$team_key],
]);

($role->$team_key ?: '') is used earlier, but doesn't scale to that array...

@Cbrad24 could you test #2457? thanks

@erikn69 appears to be working on your branch with Model::preventAccessingMissingAttributes() enabled, nice work ๐Ÿ‘๐Ÿผ

drbyte commented

Thanks guys!

drbyte commented

Closed by Pull Request #2457

drbyte commented

Fix released as v5.10.2