Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在laravel Controller中声明不同的数据库连接_Laravel_Controller_Laravel 5.4 - Fatal编程技术网

如何在laravel Controller中声明不同的数据库连接

如何在laravel Controller中声明不同的数据库连接,laravel,controller,laravel-5.4,Laravel,Controller,Laravel 5.4,我有一个控制器,我需要为我的查询生成器设置数据库, 一切正常,但当我创建新函数时,我需要重新声明连接, 我需要的是声明连接,以便整个控制器将与该数据库连接 class CompanyInformationController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ pub

我有一个控制器,我需要为我的查询生成器设置数据库, 一切正常,但当我创建新函数时,我需要重新声明连接, 我需要的是声明连接,以便整个控制器将与该数据库连接

class CompanyInformationController extends Controller
{



    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function firstFunction()
    {

     $connection = DB::connection('fdis_1');

      return  $connection->getDatabaseName();

    }

    public function secondFunction()
    {
     // This is redundant
     $connection = DB::connection('fdis_1');

      return  $connection->getDatabaseName();

    }
}

在控制器上的类中

 private $connection;

 public function __construct()
    {
        $this->connection = DB::connection('fdis_1');

    }
现在在你的方法中使用

  $this->connection->getDatabaseName();

在控制器上的类中

 private $connection;

 public function __construct()
    {
        $this->connection = DB::connection('fdis_1');

    }
现在在你的方法中使用

  $this->connection->getDatabaseName();

在构造中声明连接并将其用于整体controller@RavindraBhanderi你们有例子吗?是的,我为你们写ans,在结构中声明连接,并使用到整体中controller@RavindraBhanderi你有例子吗?是的,我为你写ansyou@MartneyAcha Welcome@MartneyAcha欢迎