无法使用laravel 5.3中的手动设置与数据库连接

时间:2016-10-11 14:44:37

标签: laravel laravel-5.3

我想在运行时连接多个数据库,所以我在运行时设置连接属性但是我遇到了错误。

这是我的控制器:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\Facade;


use App\Http\Requests;
use App\central_db;
use App\barcodedb;

class check_subsController extends Controller
{
    public function fetch(Request $request){



        if($db=\App\central_db::select(['db_name', 'first_name', 'last_name'])->where('device_srno','=',$request->srno)->get()){

            foreach ($db as $record) {
                Config::set('database.connections.wesearch_barcodedb.username', $record ->db_name);
                Config::set('database.connections.wesearch_barcodedb.password', $record ->last_name);
                Config::set('database.connections.wesearch_barcodedb.database', $record ->first_name);

            }

            if($users=\App\barcodedb::where('park','=','1')->get()){

                return $users;
            }
        }

    }
}

这是我的模特:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class barcodedb extends Model
{
    protected $connection ='wesearch_barcodedb';
    protected $table = 'user';
}

但是我收到了以下错误: " check_subsController.php第24行中的FatalThrowableError: Class' App \ Http \ Controllers \ Config'找不到"

1 个答案:

答案 0 :(得分:3)

您需要添加Config facade命名空间。

use Config;