【发布时间】:2017-06-19 20:39:48
【问题描述】:
遇到作业无法连接到数据库的问题。
Invalid catalog name: 1046 No database selected
我需要在作业中设置帐户,因此我有一个扩展类来确保帐户与作业一起发送,以便我可以确保数据库可以连接到正确的数据库。
<?php
namespace App\Jobs;
use Illuminate\Support\Facades\DB;
abstract class Job
{
protected $account;
public function start()
{
// runs when creating the job, so the config holds the correct value
$this->account = config('database.connections.tenant.database');
}
public function handle()
{
// since the handle function runs outside of setting the job
// the database is no longer set in the config
config()->set('database.connections.tenant.database', $this->account);
// try to force it to reconnect incase it already did for some reason.
DB::reconnect();
}
}
这是我正在玩的当前版本,变化似乎不影响它。我基本上在构造函数中运行start,然后确保它在作业中运行父handle,以便引导正确的数据库配置。
我正在寻找的最终结果是将租户数据库设置为account,并在运行作业时使用该数据库进行所有查询。
【问题讨论】:
标签: php laravel laravel-5 multi-tenant