The problem is that you don't switch back to the default connection on the end of your job. The result is that the next job use the connection from your tenant.
I don't understand, because what I do is get the current tenant in the central connection and then set the connection for this tenant, where all the processing must be done.
if (isset($event->job->payload()['tenant'])) { $company = DB::connection('mysql')->table('tenants')->where('domain', $event->job->payload()['tenant'])->first(); $manager = app(ManagerTenant::class); $manager->setConnection($company); }
Any suggestions or could you tell me how to solve this?
Thanks in advance.
I personal should use a second connection for the tenant models. But that result in more work (see: https://laravel.com/docs/9.x/eloquent#database-connections for the work with Eloquent)
Or you can use the JobProcessed event to roll back your database connection change.
Maybe is this enough for that:
$this->app['events']->listen(\Illuminate\Queue\Events\JobProcessed ::class, function ($event) {
if (isset($event->job->payload()['tenant'])) {
Config::set('database.default', '<yourdefault>');
// maybe disconnect your tenant database connection.
DB::purge('tenant');
DB::disconnect('tenant');
Schema::connection('tenant')->getConnection()->disconnect();
}
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community