1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class SomeController extends BaseController { public function someMethod() { $someModel = new SomeModel; $someModel->setConnection('mysql2'); // non-static method $something = $someModel->find(1); $something = SomeModel::on('mysql2')->find(1); // static method return $something; } } |
Create another class to extend model
1 2 3 4 5 6 7 8 9 10 11 |
namespace App\Models; use Illuminate\Database\Eloquent\Model; class ChangeSiteM extends Model { //protected $connection = 'mysql-tw'; public function __construct(){ $this->connection = 'mysql-tw'; } } |
Reference: