简体   繁体   中英

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. I need to define table name in constant and use it in Model.

I wrote in Model:

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

And In constant.php inside Config:

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.

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:

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');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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