0

I have a Laravel project with a local and remote DB. I have these set in the .env. When trying to access the remote DB, Laravel is attempting to use the local DB and I am getting an error. The crazy thing is that if I remove the password for remote DB in my .env file, it throws an error about access denied to the remote DB.

I read that if there is an error with the secondary DB that it would revert to the default DB, which looks like what is happening. However, I am connected to the remote DB with Sequel Pro, so I know my connection is good. Here is the .env:

APP_ENV=local
APP_DEBUG=true
APP_KEY=JnsC2R88qLM6GTnivxLWPKs2ds2dfplI

SITE_URL=http://health.dev

DB_REMOTE_HOST=myhost.cc
DB_REMOTE_DATABASE=databasename
DB_REMOTE_USERNAME=username 
DB_REMOTE_PASSWORD=password

DB_LOCAL_HOST=localhost
DB_LOCAL_DATABASE=health
DB_LOCAL_USERNAME=homestead
DB_LOCAL_PASSWORD=secret

And my database.php:

 'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_LOCAL_HOST', 'localhost'),
        'database'  => env('DB_LOCAL_DATABASE', 'forge'),
        'username'  => env('DB_LOCAL_USERNAME', 'forge'),
        'password'  => env('DB_LOCAL_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],
    'mysql_remote' => [
        'driver'    => 'mysql',
        'host'      => env('DB_REMOTE_HOST', ''),
        'database'  => env('DB_REMOTE_DATABASE', ''),
        'username'  => env('DB_REMOTE_USERNAME', ''),
        'password'  => env('DB_REMOTE_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

And my Center model:

<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;

class Center extends Model
{
    protected $table = 'Center';
    private $center;

    public function __construct()
    {
        $this->center = DB::connection('mysql_remote')->table($this->table);
    }

}
3
  • The name passed to the connection method should correspond to one of the connections listed in your config/database.php configuration file Commented Dec 21, 2015 at 18:52
  • @Adamnick I am thinking that it does if you see my config. I could be missing something, but that appears correct to me. Commented Dec 21, 2015 at 18:54
  • I totally wished I knew who downvoted my post and could give me reasonable answer as to why. I'm sure they want and will hide behind the mask of the internet, but whatever. It seems like people just get downvote happy with no real reason. Commented Dec 21, 2015 at 19:34

2 Answers 2

2

no need to pass in the constructor. You can set the connection via protected property in the model:

class Center extends Model
{
    protected $table = 'Center';

    protected $connection = 'mysql_remote';

}
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use the following method

class SomeController extends BaseController {

    public function someMethod()
    {
        $someModel = new SomeModel;

        $someModel->setConnection('mysql2');//main line

        $something = $someModel->find(1);

        return $something;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.