how to change Laravel database name in config file dynamically. i know the value for database, after some function is executed in controller. after change config value i can able access the new value from other controllers too.
'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,
],
in my app i have 3 databases. every time i create a new database dynamically after deleting previous one. so that time only i have database name with timestamp. in database config i only have empty value for db name.
Related
This question already has answers here:
How to use multiple databases in Laravel
(7 answers)
Closed last month.
How can i connect second database in model?
.env file
DB_HOST=localhost
DB_DATABASE=crmgen_lara1
DB_USERNAME=crmgen_lara1
DB_PASSWORD=Y.vfdsf
DB_HOST=localhost
DB_DATABASE=crmgen_restorant
DB_USERNAME=crmgen_restorant
DB_PASSWORD=#X(dsfs}
Database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'crmgen_lara1'),
'username' => env('DB_USERNAME', 'crmgen_lara1'),
'password' => env('DB_PASSWORD', 'Y.ıquwewqe'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'crmgen_restorant'),
'username' => env('DB_USERNAME', 'crmgen_restorant'),
'password' => env('DB_PASSWORD', '#X(sadsa}'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
],
TestModel.php
$testt = new TestModel();
$testt= TestModel::on('mysql2')->get();
dd($testt);
die();
ERROR: Table 'crmgen_lara1.test' doesn't exist (SQL: select * from test)
Because I can not connect second database. How can I solve this problem?
You just have to differentiate the different keys of the .env, because the values replace each other. DB_HOST, DB_HOST2 etc...
I have use multiple database in my project. But is that possible to use multiple connection in single query in laravel? This is my connection config.
'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' => false,
'engine' => null,
],
'mysql_pdd' => [
'driver' => 'mysql',
'host' => env('DB_PDD_HOST', '127.0.0.1'),
'port' => env('DB_PDD_PORT', '3306'),
'database' => env('DB_PDD_DATABASE', 'forge'),
'username' => env('DB_PDD_USERNAME', 'forge'),
'password' => env('DB_PDD_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
I want to join a tabel in my mysql connection to mysql_pdd table. Is that possible?
Yes, you can but for that you need to put condition, when you want to connect 'mysql' and when 'msql_pdd'.
if (mysql_connection){
$mysql =DB:: reconnect('mysql');
}else if(mysql_pdd_connection){
$mysql_pdd =DB:: reconnect('mysql_pdd');
}
Using $mysql & $mysql_pdd you can perform joins as well by code not by query.
I need to use several databases in laravel, and i'm trying to create a form to add a new db connection to connections array in config/database.php.
I don't want to add connections manually, but throught a form.
'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', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
Thanks
You simple add as many databases to the array as you require then add the connection values to you .env file or to the array valyes like so:
return array(
'default' => 'my_first_db',
'connections' => array(
# Our primary database connection
'my_first_db' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'my_second_db' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
Schema::connection('my_second_db')->create('a_new_table', function($table)
{
$table->increments('id'):
});
$users = DB::connection('my_second_db')->select('users');
To add connections through a form you would need to post the data to a template similar to the above and then write to the server, This is a bit of a security risk doing that and would need to be carefull in what you do and how you go about doing it, Posting database data in a request and then just saving the database data to a file is a bit overboard.
You could either setup a config file that has 10 databases pre-defined with PDO and then you could overwrite the settings or save to one of the config files that have no settings assigned.
You can use the config helper and set the config dynamically:
config([
'database.connections.mysql.database' => $dynamicDB,
'database.connections.mysql.username' => $dbUsername,
'database.connections.mysql.password' => $dbPassword,
// Any other dynamically set variables
]);
You could store the parameters in the session and default them to your config file:
config([
'database.connections.mysql.database' => session('db_database', config('database.connections.mysql.database')),
'database.connections.mysql.username' => session('db_username', config('database.connections.mysql.username')),
'database.connections.mysql.password' => session('db_password', config('database.connections.mysql.password')),
// Any other dynamically set variables
]);
Doing this in middleware would give you a little more control over where/when these parameters are being set and used.
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,
],
My shared web host have some problems with query prepares and I want to enable PDO's emulated prepares, there's no option for this in the config\database.php.
Is there any way I can do that in Laravel?
You can add an "options" array to add options to your Database Connection within config/database.php:
'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,
// Additional options here
'options' => [PDO::ATTR_EMULATE_PREPARES => true, PDO::MYSQL_ATTR_COMPRESS => true,]
],
You will see I've also switched on MYSQL_ATTR_COMPRESS for my connection.
You can find more information on some of the options they have built in here:
https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Connectors/Connector.php