jeudi 19 novembre 2015

Laravel 5.1, how to have a dynamic database connection in model

I`m using a self created package. This package includes some models. These models should connect to the APP database, but because its a package, the database names can be different.

To solve this, my package includes a config file, where the database name(connection name) is stored.

In the config file:

return [ 'app_model_db_connection' => 'first_database' ];

This works fine, i can grab the config value by doing: Config::get('myconfig.app_model_db_connection);

Now i wanted to do this in my model:

protected $connection = \Config::get('package_customview.app_model_db_connection');

But this does not work. The error: syntax error, unexpected '(', expecting ',' or ';'

Looks like i can only add a string after $connection = . Because when i do: protected $connection = "first_database" , it works. But i want to grab this value from my config file. Is this somehow possible?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire