简体   繁体   English

Laravel 5常量定义

[英]Laravel 5 Constant define

I tried all the options. 我尝试了所有选项。 Still, it is showing "Constant expression contains invalid operations". 尽管如此,它仍显示“常量表达式包含无效操作”。 I am using Laravel 5.5, Please Help. 我正在使用Laravel 5.5,请帮助。 I need to define table name in constant and use it in Model. 我需要在常量中定义表名并在Model中使用它。

I wrote in Model: 我在Model中写道:

protected $table = Config::get('constants.dbTable.EMAILTEMPLATE');

And In constant.php inside Config: 并在Config里面的constant.php中:

return [ 'langs' => 
    [ 
        'es' => 'www.domain.es', 
        'en' => 'www.domain.us' // etc 
    ], 
    'siteTitle' => 'HD Site', 
    'pagination' => 5, 
    'tagLine' => 'Do the best', 
    'dbTable'=>[ 
        'EMAILTEMPLATE' => 'stmd_emailTemplate'
    ] 
];

I want to use emailTemplate table. 我想使用emailTemplate表。

Based on the code you have posted in the comment, you are trying to assign a value into a property in your model but you are assigning it too early (assumed from the keyword protected .) You can't do this: 根据您在注释中发布的代码,您正在尝试为模型中的属性分配值,但分配的时间过早(假设使用关键字protected 。)您不能这样做:

class SomeModel extends Model
{
    protected $someProperty = config('some.value'); // Too early!
}

because you are trying to initialize a property that requires a run-time interpretation. 因为您正在尝试初始化需要运行时解释的属性。

There's a workaround; 有一个解决方法。 use your constructor. 使用您的构造函数。

class SomeModel extends Model
{
    protected $someProperty; // Define only...

    public function __construct() {
        parent::__construct(); // Don't forget this, you'll never know what's being done in the constructor of the parent class you extended
        $this->someProperty = config('some.value');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM