Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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 8通过配置访问model relationshipt中的数据库名称_Laravel - Fatal编程技术网

我正在尝试使用laravel 8通过配置访问model relationshipt中的数据库名称

我正在尝试使用laravel 8通过配置访问model relationshipt中的数据库名称,laravel,Laravel,您好,我正在尝试使用配置访问数据库名称,但不幸的是,它不起作用,请帮助我如何解决这个问题,谢谢 物业模型 public function editedBy() { return $this->belongsToMany('App\User',config('database.connections.web.database'), 'property_id', 'user_id')->withTimestamps(); } app/config/da

您好,我正在尝试使用配置访问数据库名称,但不幸的是,它不起作用,请帮助我如何解决这个问题,谢谢

物业模型

 public function editedBy()
    {
        return $this->belongsToMany('App\User',config('database.connections.web.database'), 'property_id', 'user_id')->withTimestamps();
    }
app/config/database/

 'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

   
        'web' => [
            'driver' => 'mysql',
            'host' => env('DB_WEB_HOST'),
            'port' => env('DB_WEB_PORT'),
            'database' => env('DB_WEB_DATABASE'),
            'username' => env('DB_WEB_USERNAME'),
            'password' => env('DB_WEB_PASSWORD'),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

    ],

您可以尝试使用env('DB_database')来命名数据库。它显示您已经更改了默认变量,所以在您的情况下可能是这样的

  public function editedBy()
        {
            return $this->belongsToMany('App\User',env('DB_WEB_DATABASE'), 'property_id', 'user_id')->withTimestamps();
        }
或者,如果你真的想使用配置,这就是一个例子

 public function getDatabaseName()
{
    $databaseName = Config::get('database.connections.'.Config::get('database.default'));

    dd($databaseName['database']);
}

显示错误是什么?