Laravel configuration of "database.php" file not works - laravel

i want to connect to database using database.php file, but it's not works.
I do not have the .env file.
I have to configure something more, beyond the file database.php
file database.php:
<?php
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'my_database'),
'username' => env('DB_USERNAME', 'mydatabase'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
],
]
]

Some of you database connection values are set to take from env file. Since you are not using env, you have to set them in the database.php file manually.
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL', 'Enter your db url here'), <--
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'Your db name'), <--
'username' => env('DB_USERNAME', 'Db user name'), <--
'password' => env('DB_PASSWORD', 'Db password'), <--
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
],
]
]
Note: You might need to change these settings again for deployment. And you have serve it after changing the file.

Related

How to change table prefix when a user logs in to account?

I am in new laravel, In my application When a user logs into his account I want to change the default database prefix from the env file. I setup wildcard subdomain for every users. When every user logs in I want to change the database prefix according to the sub domain.
So is it possible to change the laravel prefix globally if a user logs in Laravel?. Please suggest me if there is any solution. Thanks is advance.
Of course you can:
config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql_earth' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_EARTH_DATABASE', 'earth'),
'username' => env('DB_EARTH_USERNAME', 'earth'),
'password' => env('DB_EARTH_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql_moon' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_MOON_DATABASE', 'moon'),
'username' => env('DB_MOON_USERNAME', 'moon'),
'password' => env('DB_MOON_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=forge
DB_USERNAME=forge
DB_PASSWORD=forgepw
DB_EARTH_DATABASE=earth
DB_EARTH_USERNAME=erth
DB_EARTH_PASSWORD=earthpw
DB_MOON_DATABASE=moon
DB_MOON_USERNAME=moon
DB_MOON_PASSWORD=moonpw
Use in your Controller example put your conditions on the connection and then you can have the query in each database:
$db = \DB::connection('mysql');
$db = \DB::connection('mysql_earth');
$db = \DB::connection('mysql_moon');
$products = $db->table('products')
->distinct()
->select("*" )
->orderBy('products.id','asc')
->get();

How to use multiple database in Lumen

We've using Lumen for building API's , Now we need to access multiple databases.
Currently using .env for database config but unable to found the way to multiple databases in .env
where we need to read 2nd connection ...
First, you'll need to configure your connections. If you don't already have one you'll need to create a config directory in your project and add the file config/database.php. It might look like this:
<?php
return [
'default' => 'accounts',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB2_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB2_DATABASE'),
'username' => env('DB2_USERNAME'),
'password' => env('DB2_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
];
Once you've added your connection configurations, you can access them by getting the database manager object out of the container and calling ->connection('connection_name').
// Use default connection
app('db')->connection()->select('xx');
DB::connection()->select('yy');
// Use mysql2 connection
app('db')->connection('mysql2')->select('xx');
DB::connection('mysql2')->select('yy');
Hope this helps you!!
This also worked. In the current version of Lumen 5.7
config/database.php
<?php
return [
'default' => env('DB_CONNECTION', 'sqlsrv'),
'migrations' => 'migrations',
'connections' => [
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
'sqlsrv2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
];
.env
DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=database1
DB_USERNAME=username
DB_PASSWORD=password
DB_DATABASE2=database2
Usage:
Model: protected $connection = 'sqlsrv2';
Other: ->connection('sqlsrv2')
I hope i help you!
Reference:https://fideloper.com/laravel-multiple-database-connections

How to build a custom connection outside of database.php in Laravel 5.4

I am building a system that uses multiple databases. I currently have a custom config file which I am using that has some controls in it. This file is not put through version control.
I would like these databases to be independant of git. I am looking to build a custom connection without using config/database.php
I could of course remove config/database.php from git but I want to keep it neat and make use of my custom config file.
Here is my clientconfig.php to be found in /config folder
<?php
$clientDB = '';
if(isset($_SERVER['SERVER_NAME'])) {
$apiDomain = $_SERVER['SERVER_NAME'];
if ( $apiDomain == 'www.example.com' ) {
$clientDB = 'clientdb_1';
}
}
return [
'client_db' => $clientDB
];
I would like to add my connections in that file too that are found in database.php
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'example'),
'username' => env('DB_USERNAME', 'example'),
'password' => env('DB_PASSWORD', 'example'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'clientdb_1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => ('clientdb_1'),
'username' => env('DB_USERNAME', 'example'),
'password' => env('DB_PASSWORD', 'example'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Is there a clean way this can be done?
EDIT: I have consired the .env file but it is too messy considering I may have different amount of databases etc. I will still be adding them to database.php which is what I am trying to avoid.
I was able to add my own new connection in that file by using config().
if ( $apiDomain == 'www.example.com' ) {
$appUrl = $apiDomain;
$clientDB = 'clientdb_1';
config(['database.connections.clientdb_1' => array(
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => $clientDB,
'username' => 'example',
'password' => 'example',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
)]);
}
NB: This did not work unless my custom config file was loaded after database.php. Because it was in alphabetical order I had to rename my file to z_clientconfig.php

Do i need to change database configuration in database.php file in laravel when upload to server?

Do i need to change database configuration in database.php file in laravel when upload to server?
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Don't change anything in database.php config file. What you need to do is to change DB credentials in .env file on server side.
So, you'll have different .env files on a local machine and server, but the same database.php config file.
https://laravel.com/docs/5.3/configuration#environment-configuration
Update your database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'yourdatabasename'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],

Troubleshouting laravel migrations for new database

I am trying to set a separate database for tests. For some reason when I run the migrations I have always a message saying: "Nothing to migrate."
Here is how I run the migrations:
php artisan migrate --database=mysql_test
Nothing to migrate.
And here is my config/database.php:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydb'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'dev'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_test' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydbtest'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'dev'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
And obviously I can login to the database mydbtest with the credentials in the config.
Updating here after some troubleshooting:
After emptying mydb database and running the same command it filled the mydb database not mydbtest. This means it does not take into account what I am giving as database connection name.
Any suggestions to troubleshoot this are welcome.
Thanks
I found the problem.
After commenting out this line in my .env file it works now:
#DB_DATABASE=mydb
Thanks

Resources