在Laravel中获得正确的路径

在Laravel中获得正确的路径,laravel,laravel-5,Laravel,Laravel 5,我在custom目录App中有自定义文件。其中一个文件名为update.php,因此路径为 App\custom\update.php 在update.php中,我有 $config = include __DIR__ . '/../../app/config/database.php'; $servername = $config['connections']['mysql']['host']; $username = $config['connections']['mysql']['

我在
custom
目录
App
中有自定义
文件。其中一个文件名为update.php,因此路径为

App\custom\update.php
在update.php中,我有

$config = include __DIR__ . '/../../app/config/database.php';

$servername = $config['connections']['mysql']['host'];
$username   = $config['connections']['mysql']['username'];
$password   = $config['connections']['mysql']['password'];
$dbname     = $config['connections']['mysql']['database'];
它读取数据库凭据。这在Laravel4.2上非常有效,但在Laravel5.4中不起作用

对于Laravel 5.4,我已将路径更改为

$config = include __DIR__ . '/../../../config/database.php';
但是错误显示此
。/../../
而不是执行它

include(/var/www/html/site/app/custom/../../../config/database.php): failed to open stream: No such file or directory
我怎样才能给出正确的路径

更新:当前代码
database.php

    'mysql' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'port' => '3306',
        'database' => 'test',
        'username' => 'root',
        'password' => '123321',
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
在update.php中

$config = config_path('database.php');

$servername = config('database.connections.mysql.host');
$username = config('database.connections.mysql.host');
$password = config('database.connections.mysql.host');
$dbname = config('database.connections.mysql.host');


$conn = new mysqli($servername,$username,$password,$dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

您不必包含该文件,而是使用helper方法


如果确实要包含它,可以使用
config\u path()


谢谢,但是当我吼的时候,$conn=newmysqli($config)
我得到了
mysqli::\uu构造():php\u network\u getaddrinfo失败:名称或服务未知
谢谢,错误是
mysqli::\uu构造():(HY000/1045):拒绝用户“localhost”@“localhost”的访问(使用密码:YES)
在此
$conn=new mysqli($servername、$username、$password、$dbname)检查您的数据库凭证我已用当前代码更新了问题。我百分之百确定这些凭证是正确的。但是为什么要使用用户localhost而不是root?
$servername = config('database.connections.mysql.host');
$path = config_path('database.php');