{"id":"4662cc79ddc2e0d7","repo":"laravel/framework","slug":"the-environment-property-must-be-set-and-cannot-be","errorCode":null,"errorMessage":"The environment property must be set and cannot be empty.","messagePattern":"The environment property must be set and cannot be empty\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Container/Attributes/Bind.php","lineNumber":43,"sourceCode":"     */\n    public array $environments = [];\n\n    /**\n     * Create a new attribute instance.\n     *\n     * @param  class-string  $concrete\n     * @param  non-empty-array<int, \\UnitEnum|non-empty-string>|non-empty-string|\\UnitEnum  $environments\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function __construct(\n        string $concrete,\n        string|array|UnitEnum $environments = ['*'],\n    ) {\n        $environments = array_filter(is_array($environments) ? $environments : [$environments]);\n\n        if ($environments === []) {\n            throw new InvalidArgumentException('The environment property must be set and cannot be empty.');\n        }\n\n        $this->concrete = $concrete;\n\n        $this->environments = array_map(\n            fn ($environment) => enum_value($environment),\n            $environments,\n        );\n    }\n}\n","sourceCodeStart":25,"sourceCodeEnd":54,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Container/Attributes/Bind.php#L25-L54","documentation":"Thrown by the Bind container attribute when the environments argument, after filtering, resolves to an empty array. The constructor accepts a string, UnitEnum, or array and runs array_filter to drop falsy values; if everything was filtered out (e.g. an empty string, null, or [null]) the attribute is meaningless and Laravel refuses to register an environment-conditional binding.","triggerScenarios":"Declaring #[Bind(SomeClass::class, environments: '')] or #[Bind(SomeClass::class, environments: [null, ''])] on a class. Also #[Bind(SomeClass::class, environments: [])] after the constructor wraps a scalar and filters it.","commonSituations":"Reading environments from a config or env variable that resolves to an empty string; passing a dynamic variable that is sometimes empty; misconfiguring a multi-environment binding where all entries were filtered.","solutions":["Pass at least one non-empty environment string or a UnitEnum case, e.g. #[Bind(Concrete::class, 'production')] or #[Bind(Concrete::class, ['testing','staging'])].","Use the wildcard default #[Bind(Concrete::class)] (defaults to ['*']) if you want the binding to apply everywhere.","If environments come from config/env, validate they are non-empty before using the attribute, or fall back to the wildcard."],"exampleFix":"// before\n#[Bind(PaymentGateway::class, environments: config('app.bind_env'))]\nclass Gateway {}\n\n// after\n#[Bind(PaymentGateway::class, environments: 'production')]\nclass Gateway {}","handlingStrategy":"validation","validationCode":"$envs = array_filter((array) $environments);\nif ($envs === []) {\n    throw new \\InvalidArgumentException('environments must contain at least one non-empty value');\n}\nnew \\Illuminate\\Container\\Attributes\\Bind($concrete, $envs);","typeGuard":"function isValidEnvironmentList(string|array|\\UnitEnum $envs): bool\n{\n    return array_filter(is_array($envs) ? $envs : [$envs]) !== [];\n}","tryCatchPattern":"try {\n    #[Bind(Concrete::class, environments: $dynamicEnv)]\n    class X {}\n} catch (\\InvalidArgumentException $e) {\n    // fall back to wildcard binding\n}","preventionTips":["Default to the ['*'] wildcard when unsure.","Validate dynamic env values from config before applying the attribute (attributes are evaluated at reflection time).","Use UnitEnum cases for environments to guarantee non-empty values."],"tags":["container","attributes","binding","configuration"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}