Inside of my database configuration file - likely app/config/database.php.
'connections' => [
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '192.168.0.5'),//'ADMINRG-2UH26BG', // Provide IP address here
'database' => env('DB_DATABASE', 'VHPDEV'),
'username' => env('DB_USERNAME', 'sa'),
'password' => env('DB_PASSWORD', 'Dbp#2013'),
'prefix' => '',
],
'sqlsrv2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '192.168.0.5'),//'ADMINRG-2UH26BG', // Provide IP address here
'database' => env('DB_DATABASE', 'OCSDB'),
'username' => env('DB_USERNAME', 'sa'),
'password' => env('DB_PASSWORD', 'Dbp#2013'),
'prefix' => '',
],
],
.env
DB_CONNECTION=sqlsrv
DB_HOST=192.168.0.5
DB_PORT=3306
DB_DATABASE=VHPDEV
DB_USERNAME=sa
DB_PASSWORD=Dbp#2013
Query
DB::connection('sqlsrv')->table('B14SILHOLD')->get();
You can just clear cache of laravel by running command such as:- php artisan config:cache and try to execute it.
Also remember that check that your DB is defined in your model where you are using it or just put \DB::connection('sqlsrv')->table('B14SILHOLD')->get();
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...
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
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,
],
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
i have laravel 5 and it works fine on browser.After then for making table in database, firstly i have create directory i.e /opt/lampp/htdocs/laravel, then run php artisan migrate:install command but i have got message shown below. How to solve this problem.
cd /opt/lampp/htdocs/lovey
/opt/lampp/htdocs/lovey$ php artisan migrate:install
Application In Production! *
Do you really wish to run this command? [y/N] (yes/no) [no]:
HERE IS MY database.php file.
my database name is laravel.
return
[
'fetch' => PDO::FETCH_CLASS,
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
'migrations' => 'migrations',
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
];
Your .env is required for laravel to run properly. The .env file stores your enviroment configuration, your database configuration included.
http://laravel.com/docs/5.1/installation#environment-configuration
Example of .env
APP_ENV=local
APP_DEBUG=true
APP_KEY=J9dFWi5QPq2aIuAX9XAfnuOoKVmNsFHs
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
If you create your laravel project with composer:
composer create-project laravel/laravel --prefer-dist
A .env will be created. If you have created any other way .env may not have been created, but you will have a file named .env.example, you can copy/rename the file to .env and your laravel instalation should work fine.
Also if your application is in production you should change your APP_* parameters.