spatie/laravel-permission

Throwing error on migrate Access to undeclared static property Spatie\Permission\PermissionRegistrar::$pivotPermission

biswajitpaul01 opened this issue · 2 comments

Describe the bug
When I am trying to make a fresh migrate from an existing project on my local, I am getting error like: Access to undeclared static property Spatie\Permission\PermissionRegistrar::$pivotPermission

Versions
You can use composer show to get the version numbers of:

  • spatie/laravel-permission package version: "^6.0"
  • illuminate/framework package: "^10.10"

PHP version: 8.1.25

Database version: 8.2.12

Error
After php artisan migrate command:

sail@f76ada990e51:/var/www/html$ php artisan migrate

   INFO  Preparing database.  

  Creating migration table .............................................................................................................. 141ms DONE

   INFO  Running migrations.  

  2014_10_12_000000_create_users_table .................................................................................................. 202ms DONE
  2014_10_12_100000_create_password_reset_tokens_table .................................................................................. 118ms DONE
  2016_06_01_000001_create_oauth_auth_codes_table ....................................................................................... 120ms DONE
  2016_06_01_000002_create_oauth_access_tokens_table .................................................................................... 294ms DONE
  2016_06_01_000003_create_oauth_refresh_tokens_table ................................................................................... 213ms DONE
  2016_06_01_000004_create_oauth_clients_table ........................................................................................... 79ms DONE
  2016_06_01_000005_create_oauth_personal_access_clients_table ........................................................................... 21ms DONE
  2019_08_19_000000_create_failed_jobs_table ............................................................................................. 73ms DONE
  2019_12_14_000001_create_personal_access_tokens_table .................................................................................. 56ms DONE
  2023_10_16_172019_create_permission_tables ............................................................................................. 86ms FAIL

   Error 

  Access to undeclared static property Spatie\Permission\PermissionRegistrar::$pivotPermission

  at database/migrations/2023_10_16_172019_create_permission_tables.php:54
     50▕             }
     51▕         });
     52▕ 
     53▕         Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) {
  ➜  54▕             $table->unsignedBigInteger(PermissionRegistrar::$pivotPermission);
     55▕ 
     56▕             $table->string('model_type');
     57▕             $table->uuid($columnNames['model_morph_key']);
     58▕             $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');

      +4 vendor frames 

  5   database/migrations/2023_10_16_172019_create_permission_tables.php:75
      Illuminate\Support\Facades\Facade::__callStatic()
      +24 vendor frames 

  30  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

docker-compose.yml

services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
            IGNITION_LOCAL_SITES_PATH: '${PWD}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - phpmyadmin
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - mysqladmin
                - ping
                - '-p${DB_PASSWORD}'
            retries: 3
            timeout: 5s
    phpmyadmin:
        image: 'phpmyadmin:latest'
        ports:
            - 8080:80
        networks:
            - sail
        environment:
            - PMA_ARBITRARY=1
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local

composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The skeleton application for the Laravel framework.",
    "keywords": ["laravel", "framework"],
    "license": "MIT",
    "require": {
        "php": "^8.1",
        "guzzlehttp/guzzle": "^7.2",
        "laravel/framework": "^10.10",
        "laravel/passport": "^11.9",
        "laravel/sanctum": "^3.2",
        "laravel/tinker": "^2.8",
        "spatie/laravel-medialibrary": "^10.13",
        "spatie/laravel-permission": "^6.0"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "^3.9",
        "fakerphp/faker": "^1.9.1",
        "laravel/breeze": "^1.25",
        "laravel/envoy": "^2.8",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.26",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^7.0",
        "phpunit/phpunit": "^10.1",
        "spatie/laravel-ignition": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true,
            "php-http/discovery": true
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}

I'm glad to help you, were able to solve the issue by recreating the migration for the permission table. This is a common error that can occur if you have upgraded the Spatie\Permission package to a newer version without updating your migration files.

To avoid this error in the future, it is important to keep your migration files up to date with the latest versions of the packages you are using. You can do this by running the following command:

'php artisan migrate --package=spatie/permission'
This will create new migration files for the Spatie\Permission package, if necessary. You can then migrate your database to the new version by running the following command:

php artisan migrate
If you are still having problems with the Spatie\Permission package, please feel free to ask me

Additional notes:

You can also try clearing the permission cache before migrating your database. To do this, run the following command:
php artisan cache:clear
If you are using a custom permission model, you will need to update your migration files to reflect the changes. For example, if you are using a model named MyPermission instead of Permission, you will need to replace all instances of Permission in your migration files with MyPermission.

I hope this helps!

This specific issue is mentioned in the Upgrade Documentation at:
https://spatie.be/docs/laravel-permission/v6/upgrading

Screen Shot 2023-11-05 at 4 08 18 PM