Reputation:
I have custom files
in directory custom
inside App
. One file is called update.php, so path is
App\custom\update.php
In update.php I have
$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'];
Which reads database credentials. This works perfectly on Laravel 4.2 but doesn't work in Laravel 5.4
For Laravel 5.4 I've changed the path to
$config = include __DIR__ . '/../../../config/database.php';
But the error shows this ../../../
instead to execute it
include(/var/www/html/site/app/custom/../../../config/database.php): failed to open stream: No such file or directory
How can I proper give the path?
Update: current code 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,
],
in 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);
}
Upvotes: 0
Views: 39
Reputation: 1036
If you really want to include it, you could use config_path()
:
$path = config_path('database.php');
Upvotes: 0
Reputation: 7972
You don't have to include the file, rather use the helper method config()
$servername = config('database.connections.mysql.host');
Upvotes: 1