spatie/laravel-permission

[6.0.0] Cannot assign null to property Spatie\Permission\PermissionRegistrar::$teamsKey of type string

geniqtech opened this issue · 4 comments

Was trying to upgrade from v5.11.1 to v6.0.0 but wasn't able to complete the upgrade process.

Versions

  • spatie/laravel-permission package version: 5.11.1
  • laravel/framework package: 10.30.1
  • PHP version: 8.2.3

To Reproduce
Steps to reproduce the behavior:

  1. Update composer.json => "spatie/laravel-permission": "^6.0.0"
  2. Run "composer update"
  3. The following error occurs:
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Upgrading spatie/laravel-permission (5.11.1 => 6.0.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Downloading spatie/laravel-permission (6.0.0)
  - Upgrading spatie/laravel-permission (5.11.1 => 6.0.0): Extracting archive
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   TypeError

  Cannot assign null to property Spatie\Permission\PermissionRegistrar::$teamsKey of type string

  at vendor\spatie\laravel-permission\src\PermissionRegistrar.php:69
     65▕     {
     66▕         $this->cacheExpirationTime = config('permission.cache.expiration_time') ?: \DateInterval::createFromDateString('24 hours');
     67▕
     68▕         $this->teams = config('permission.teams', false);
  ➜  69▕         $this->teamsKey = config('permission.column_names.team_foreign_key');
     70▕
     71▕         $this->cacheKey = config('permission.cache.key');
     72▕
     73▕         $this->pivotRole = config('permission.column_names.role_pivot_key') ?: 'role_id';

  1   vendor\spatie\laravel-permission\src\PermissionRegistrar.php:61
      Spatie\Permission\PermissionRegistrar::initializeCache()

  2   [internal]:0
      Spatie\Permission\PermissionRegistrar::__construct()

Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

Hmmm. We might need to code a fallback for the missing key.

As a workaround, I'd suggest taking the config file from this repo and merge its updates into your own app's old config file.
That should get around the issue.

Please report back, as your feedback will help determine the best "fix" to build into the package.

@drbyte Thank you very much for your response.
Following your advice, I added the following to permissions.php (under section "column_names") and the problem was solved.

/*
 * Change this if you want to use the teams feature and your related model's
 * foreign key is other than `team_id`.
 */

 'team_foreign_key' => 'team_id',

@erikn69 Do you think it's best to just declare public string $teamsKey; as nullable? Or have the config lookup default to 'team_id' instead of null?

Or have the config lookup default to 'team_id' instead of null?

I think it would be better, but it shouldn't affect either

private ?string $roleClass = null;