2

I'm developing a Multi Tenant with Laravel v5.7 and I'm successful in sending queue emails, since my models have the property 'connection' defined.

But when trying to send, for example, an email using the Jobs class, the same fails and informs that the table of model does not exist.

From what error recorded in the table 'failed_jobs', even with the property 'connection' defined, it appears that the Job nevertheless tries to connect to the main database and not to the specified database of the property.

Is there any way to specify in Job which database to use, since the same is informed in the model?

database.php

'connections' => [

    'others' => ['...']

    'TENANT001' => [
        'driver' => 'mysql',
        'database' => env('TENANT001_DATABASE', ''),
        'host' => env('TENANT001_HOSTNAME', ''),
        'port' => env('DB_PORT', '3306'),
        'username' => env('TENANT001_USERNAME', 'forge'),
        'password' => env('TENANT001_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

],

Sample Model

class Template extends Model
{
/**
 * The database name used by the model.
 *
 * @var string
 */
protected $connection = 'TENANT001';
}

failed_jobs

PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'main_database.templates' doesn't exist in /www/samba/laravel.local/vendor/laravel/framework/src/Illuminate/Database/Connection.php:326
Magno Alberto
  • 628
  • 14
  • 27

1 Answers1

0

I guess you are trying to access second connection TENANT001 in Template Model.

class Template extends Model
{
/**
 * The database name used by the model.
 *
 * @var string
 */
protected $connection = 'TENANT001';

protected $table='templates';

}

After that in your config/database.php

'connections' => [

    'others' => ['...']

    'TENANT001' => [
        'driver' => 'mysql',
        'database' => env('TENANT001_DATABASE', ''),
        'host' => env('TENANT001_HOSTNAME', ''),
        'port' => env('DB_PORT', '3306'),
        'username' => env('TENANT001_USERNAME', 'forge'),
        'password' => env('TENANT001_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

Or you can use connection in the queue.php

'database' => [
        'connection' => 'TENANT001'
        'driver' => 'mysql',
        'table' => 'jobs',
        'queue' => 'default',
        'expire' => 60,
    ],

And clear the cache once you update .env files.

php artisan config:cache

And check you have define everything correctly in .env and table exists.

Thanks

Sundar Ban
  • 589
  • 5
  • 16
  • Thanks for the feedback and for your suggestion. It may be from my limited knowledge of Laravel, but while I was reading your suggestion, I was at the same time executing the command: composer dump-autoload and before testing your suggestion, I made another send and ended up working as expected. – Magno Alberto Feb 15 '19 at 12:51