如何在Laravel 5.6中快速在foreach循环中连接多个数据库

时间:2019-02-15 07:55:08

标签: php laravel-5.6

我正在尝试迁移一些数据库。在运行时,我只能连接到第一个数据库,而不能连接其他数据库。

这是我的代码。任何人都可以告诉我我的代码有什么问题。

public function database_update(Request $request)
{
    $get_service_type = ServiceType::where('id',$request->service)->get();
    $get_packages = Package::where('id',$request->package)->get();
    if(isset($get_service_type[0])){
        $service_type_name = $get_service_type[0]->name;
        $packages = [];
        foreach ($get_packages as $gp){
            $packages[$gp->id]  =   $gp->name;
        }
        $package_name = (isset($packages[$request->package])) ? $packages[$request->package] : '';
        $get_tables = MasterLayout::where([
            ['service_type','=',$request->service],
            ['package','=',$request->package],
        ])->get();
        foreach ($get_tables as $gt){
            Config::set('database.connections.project_mysql.database', $gt->database);

            DB::setDefaultConnection('project_mysql');
            Artisan::call('migrate',
                ['--path' => 'database/migrations/' . $service_type_name . '/' . $package_name]
            );
        }

        return back()->with('success_','tables updated');
    }
    else{
        return back()->with('error_','There is no record found');
    }
}

我总是得到相同的数据库名称approwsh_pharmacy_mas1547789795

0 个答案:

没有答案