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.
Related
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.
When i had one database i did :
$data=DB::table('data')->select(...)
How do I it with
$data=DB::connection('foo')->select(...);
Do it that way:
$data = DB::connection('foo')->table('data')->select(...);
Good luck!
You can just configure your as many database you want in config/database.php file,
'custom_db1' => [
'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' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
]
And Use it in eloquent Model like
protected $table = "custom_db1.table1";
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();
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
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