0

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.

Collapsed PLUG
  • 304
  • 5
  • 14
  • 2
    What have you tried. Can you post the code here. – Arunachalam E May 09 '18 at 06:29
  • 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 – Anumita Banerjee May 09 '18 at 06:46
  • Have a look at the solution given in the link. May be that might help you. https://stackoverflow.com/questions/40827870/constant-expression-contains-invalid-operations – Arunachalam E May 09 '18 at 06:52
  • Possible duplicate of [Constant expression contains invalid operations](https://stackoverflow.com/questions/40827870/constant-expression-contains-invalid-operations) – Collapsed PLUG May 09 '18 at 07:12

1 Answers1

0

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');
    }
}
Collapsed PLUG
  • 304
  • 5
  • 14