Overriding Connection Logic in Hyn Multi-tenancy Package (v5.9) for Laravel

15 views Asked by At

I'm currently working on a Laravel project utilizing the Hyn multi-tenancy package, version 5.9. My application requires customizing the database connection logic. Specifically, I need to override the part of the code in the Connection.php file responsible for setting the database connection parameters.

Here's the segment of code I'm looking to override:

case static::DIVISION_MODE_SEPARATE_DATABASE:
    $clone['username'] = $clone['database'] = $website->uuid;
    $clone['password'] = $this->passwordGenerator->generate($website);

Is there a recommended way to override these connection parameters?

1

There are 1 answers

0
Zain ul Abideen On

We can override this thing with one of the Hyn event called (ConfigurationLoaded).

Make a listener and override the connection credientials like this.

class TenantDatabaseConfigListener
{
    /**
     * Handle the event.
     *
     * @param  ConfigurationLoaded  $event
     * @return void
     */
    public function handle(ConfigurationLoaded $event)
    {
        /** @var Website $website */
        $website = $event->website;

        $event->configuration['database'] = $website->uuid;
        $event->configuration['username'] = 'your username';
        $event->configuration['password'] = 'your password';
    }
}

Then in eventServiceProvider called this listener as follow:

protected $listen = [
        'Hyn\Tenancy\Events\Database\ConfigurationLoaded' => [
            'App\Listeners\TenantDatabaseConfigListener',
        ],
    ]