Support the ongoing development of Laravel.io →
Session Database

Hey guys, Im only new to this, so sorry if its a stupid question.

Background: I have 3 databases that are the same. Im working on consolidating them, but basically I've written the Models and Controllers to work with the first database. What I'd like to do is when the user logins, they select a 'location' from dropdown and this defines what database laravel will use.

So far the login function sets the location:

Session::put('location', Input::get('location'));

I've defined my each of my connections to each database in config/database.php with the location name.

In my Models I'd like to set the following but am getting the error below:

protected $connection = Session::get('location');

Error:

	Symfony \ Component \ Debug \ Exception \ FatalErrorException
	syntax error, unexpected '(', expecting ',' or ';'
	open: /Library/WebServer/Documents/scans/app/models/Scan.php
	*
	* @var string
	*/
	protected $table = 'scans';
 
	protected $primaryKey = 'num';
 
	protected $connection = Session::get('location');

I know this isn't the best practice. But I need to leave the databases as is for now.

Thanks, Zac

Last updated 3 years ago.
0

Worked it out, need to use a constructer.

REF: http://stackoverflow.com/questions/20787621/use-session-in-eloquent-laravel

protected $connection = 'default';
    
public function __construct() {
  parent::__construct();
  $this->connection = Session::get("location");
}

Next I'm going to incorporate something so the database names aren't recorded in plain text.

Thanks, Zac

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

zfarrugia zfarrugia Joined 23 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.