spatie / laravel-enum

Laravel support for spatie/enum
https://spatie.be/open-source
MIT License
341 stars 37 forks source link

Translator Class not found inside labels method #75

Closed ahmedsayedabdelsalam closed 3 years ago

ahmedsayedabdelsalam commented 3 years ago

when i use translator function inside labels method an exception is thrown with a message that the Translator class does not exist, any Ideas?

public static function labels()
{
   return [
       'ACTIVE' => __('app.active'),
    ];
}
Gummibeer commented 3 years ago

Hey, could you please post a full stack-trace? This should only happen if the first enum instance is resolved before the translator service is registered.

In general I recommend to don't use the labels() method with translations - because labels have to be unique. https://github.com/spatie/enum/blob/0868053bbe79464ca164ab75c6a59d1f7da05709/src/Enum.php#L296-L298 Instead you could add another method to the enum or just use the enum in your views for example __('app.'.$enum).

ahmedsayedabdelsalam commented 3 years ago

Fatal error: Uncaught ReflectionException: Class translator does not exist in /Users/ahmed/Sites/php/surepay/vendor/laravel/framework/src/Illuminate/Container/Container.php:836 Stack trace:

0 /Users/ahmed/Sites/php/surepay/vendor/laravel/framework/src/Illuminate/Container/Container.php(836): ReflectionClass->__construct('translator')

1 /Users/ahmed/Sites/php/surepay/vendor/laravel/framework/src/Illuminate/Container/Container.php(717): Illuminate\Container\Container->build('translator')

2 /Users/ahmed/Sites/php/surepay/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(841): Illuminate\Container\Container->resolve('translator', Array, true)

3 /Users/ahmed/Sites/php/surepay/vendor/laravel/framework/src/Illuminate/Container/Container.php(655): Illuminate\Foundation\Application->resolve('translator', Array)

4 /Users/ahmed/Sites/php/surepay/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(826): Illuminate\Container\Container->make('translator', Array)

5 /Users/ahmed/Sites/php/surepay/ven in /Users/ahmed/Sites/php/surepay/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 838

image
Gummibeer commented 3 years ago

I'm unable to reproduce the error.

Bildschirmfoto 2021-06-08 um 14 50 42

Tried in a fresh Laravel app - everything with the latest versions.

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^7.3|^8.0",
        "fideloper/proxy": "^4.4",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "laravel/framework": "^8.40",
        "laravel/tinker": "^2.5",
        "spatie/laravel-enum": "^2.5"
    },
    "require-dev": {
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.3.3"
    },
    "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-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
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}
ahmedsayedabdelsalam commented 3 years ago

I tired different enum and it's working, I think this issue in the class only thanks for your support, I'll close the issue.