spatie/laravel-permission

Trait "Spatie\Permission\Traits\HasRoles" not found in app/User.php

MarekOtruba opened this issue ยท 10 comments

Hi, I have problems with usage of HasRoles trait. I already checked composer.lock, all installed, package exists in vendor but app cannot localise it. I don't know what else I can do more.

Versions

  • spatie/laravel-permission package version: 6.2.0
  • illuminate/framework package -not installed
  • composer version: 2.6.6

PHP version:
8.3.0 (Laravel 10.37.0)
Database version:
MariaDB 11.2.0

Here is my example code and/or tests showing the problem in my app:

use Spatie\Permission\Traits\HasRoles;
use Spatie\Permission\Models\Role;

class User extends Authenticatable
{
    use HasRoles;

COMPOSER

{
            "name": "spatie/laravel-permission",
            "version": "6.2.0",
            "source": {
                "type": "git",
                "url": "https://github.com/spatie/laravel-permission.git",
                "reference": "299dd2c9bce700ea641021c1aea0dfece25e541d"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/spatie/laravel-permission/zipball/299dd2c9bce700ea641021c1aea0dfece25e541d",
                "reference": "299dd2c9bce700ea641021c1aea0dfece25e541d",
                "shasum": ""
            },
            "require": {
                "illuminate/auth": "^8.12|^9.0|^10.0|^11.0",
                "illuminate/container": "^8.12|^9.0|^10.0|^11.0",
                "illuminate/contracts": "^8.12|^9.0|^10.0|^11.0",
                "illuminate/database": "^8.12|^9.0|^10.0|^11.0",
                "php": "^8.0"
            },
            "require-dev": {
                "laravel/passport": "^11.0",
                "orchestra/testbench": "^6.23|^7.0|^8.0|^9.0",
                "phpunit/phpunit": "^9.4|^10.1"
            },
            "type": "library",
            "extra": {
                "branch-alias": {
                    "dev-main": "6.x-dev",
                    "dev-master": "6.x-dev"
                },
                "laravel": {
                    "providers": [
                        "Spatie\\Permission\\PermissionServiceProvider"
                    ]
                }
            },
            "autoload": {
                "files": [
                    "src/helpers.php"
                ],
                "psr-4": {
                    "Spatie\\Permission\\": "src"
                }
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "MIT"
            ],
            "authors": [
                {
                    "name": "Freek Van der Herten",
                    "email": "freek@spatie.be",
                    "homepage": "https://spatie.be",
                    "role": "Developer"
                }
            ],
            "description": "Permission handling for Laravel 8.0 and up",
            "homepage": "https://github.com/spatie/laravel-permission",
            "keywords": [
                "acl",
                "laravel",
                "permission",
                "permissions",
                "rbac",
                "roles",
                "security",
                "spatie"
            ],
            "support": {
                "issues": "https://github.com/spatie/laravel-permission/issues",
                "source": "https://github.com/spatie/laravel-permission/tree/6.2.0"
            },
            "funding": [
                {
                    "url": "https://github.com/spatie",
                    "type": "github"
                }
            ],
            "time": "2023-12-09T01:40:07+00:00"
        },

composer du

composer du

@parallels999
Didn't help, used php artisan cache:clear after composer du, still showing error.

Post your composer.json file. Not the lock file.

@drbyte

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.2.5|^8.3",
        "barryvdh/laravel-ide-helper": "^2.13",
        "bugsnag/bugsnag-laravel": "^2.21.0",
        "doctrine/dbal": "^3.0",
        "infyomlabs/adminlte-templates": "6.0.0",
        "infyomlabs/laravel-generator": "6.0.1",
        "intervention/image": "^2.5",
        "jenssegers/date": "^4.0",
        "kalnoy/nestedset": "^6.0",
        "kg-bot/laravel-localization-to-vue": "^2.1",
        "laracasts/utilities": "^3.0",
        "laravel/framework": "^10.37",
        "laravel/legacy-factories": "^1.1",
        "laravel/scout": "^10.6",
        "laravel/tinker": "^2.8",
        "laravel/ui": "^4.2",
        "owen-it/laravel-auditing": "^13.0",
        "prettus/l5-repository": "^2.7",
        "proengsoft/laravel-jsvalidation": "^4.4",
        "rap2hpoutre/laravel-log-viewer": "^2.3",
        "spatie/db-dumper": "^3.4",
        "spatie/laravel-html": "^3.3.0",
        "spatie/laravel-http-logger": "^1.2",
        "spatie/laravel-permission": "^6.2",
        "symfony/process": "^6.4.x-dev",
        "tightenco/ziggy": "^2.0.0-beta.2"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fakerphp/faker": "^1.9.1",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^7.0",
        "phpunit/phpunit": "^10.5"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "Soulcore\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "SoulDatabase\\Seeders\\": "database/seeders/"
            }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan ide-helper:generate",
            "php artisan ide-helper:meta"
        ]
    }
}

Seems perhaps your composer cache is damaged.
Suggest:

rm -rf composer.lock vendor
composer install

If that doesn't solve it, then please explain:

  • when did this error begin?
  • when did you install this package?

For example, you did not explain whether this is a brand new problem immediately after installing this package, or if it "suddenly" occurred after having been using the package in this app for long time.

To "test", I created a new Laravel app, then copied your composer.json into it, and ran composer update. It gave me some errors because of your custom Soulcore namespace, but changing it back to App got rid of the errors. And the trait works correctly when added to the User model.
So, either the problem is with your app and namespaces, or your composer vendor autoload cache is damaged.

If rebuilding the composer vendor autoload (by removing the lock file and the vendor dir and running composer install or composer update) doesn't solve it, then start creating a new Laravel app which demonstrates the problem, and share it on a github repo for inspection.

@drbyte

Did you try to run the app? Because in PhpStorm everything is fine, import looks good, but when i try to log in to administration error pops out. But everything in IDE works. I tried remove lock file and vendor but it doesn't help.

image

@drbyte FIXED!

It's a bit complicated but, the app consists of 2 projects, soulcore is admin part, and when starting the main app, it has not started vendor autoload for soulcore, that's why libraries were not loaded but still existing. So I just need to call soulcore vendor autoload.php. Thanks for your advices and merry xmas!

It's highly unusual to need to manually call vendor autoload.php. But sounds like your "2nd app" is disjointed from normal structure of composer-based apps.

The screenshot from your IDE shows a lot of errors and warnings with the underlines: those underlines are always clues about things that need to be resolved.