2 weeks ago

Encapsulating a Capsule instance

Posted 2 weeks ago by turkalicious

Hello all,

I am trying to write a database model outside of laravel and I wanted to utilize the illuminate database package. In my database model class, I have

use Illuminate\Database\Capsule\Manager as Capsule;  

class DatabaseModel

    public function __construct(String $database, String $username, String $password)
        // set database name in class
        $this->dbUser = $username;
        $this->dbPassword = $password;
        // get connection handle to the db

 // Setup the Eloquent ORM
        // Make this Capsule instance available globally via static methods 
    private function setHandle($dbName)
        try {
            $this->handle = new Capsule;
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'database'  => $dbName,
                'username'  => $this->dbUser,
                'password'  => $this->dbPassword,
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ], $dbName);

        } catch (\PDOException $e) {
            return $e->getMesssage();
        } catch (Throwable $e) {
            return $e->getMessage();
public function getClientDBs()
        return $this->handle::table('SCHEMATA')
                ->select('SCHEMA_NAME as db') 

and in my library class I make two instances of this database model with two different db names

class CheckQueue 

    public function __construct(String $hourMinimum = '6')
        try { 
            if (! $this->hasSetup)

            $this->log = $this->logger->withName('Queue');
            $this->hourInterval = $hourMinimum; 

            // connect to db
            $this->ischema = new DatabaseModel('information_schema', $this->dbUser, $this->dbPassword);
            $this->client = new DatabaseModel('clientDB', $this->dbUser, $this->dbPassword);
            // // get a list of district databases 
            $this->clientDBs = $this->ischema->getClientDBs();

but then I get the error that says clientDB.SCHEMATA does not exist. If I reverse instance creations, it works because info schema becomes the latest one. I dont understand why it is using the client handle because I am calling it through the $this->ischema variable. I feel like I am missing something here. I'd greatly appreciate your inputs. Thank you

Please sign in or create an account to participate in this conversation.