How to specify Tinker to use a different database connection?
Asked Answered
S

3

6

I have two database connections. One for my application and another for testing. In my ..\config\database.php

         'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

        'testing' => [
            'driver'    => 'mysql',
            'host'      => env('DB_TEST_HOST', 'localhost'),
            'database'  => env('DB_TEST_DATABASE', 'forge'),
            'username'  => env('DB_TEST_USERNAME', 'forge'),
            'password'  => env('DB_TEST_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

I am able to change the database connection in seeding using

php artisan db:seed --database=testing

I wanted to use tinker for the connection 'testing' but unable to change it. Is there any way to change the database connection for tinker similar with database seeding?

Stingo answered 4/4, 2018 at 11:32 Comment(0)
A
17

As your question starts with using one database for testing/development and one for production, you should look into using different environments, this will allow you to have no change in your code between deployment & local testing.


This task can easily be achieved by specifying your environment:

php artisan tinker --env=local

By default, if you specify no --env, you will be using /your-app/.env

When using local you read variables from /your-app/.env.local


For your specific use case:

php artisan db:seed --env=local

Further reading for Laravel 5.1: https://laravel.com/docs/5.1/configuration

Latest version: https://laravel.com/docs/configuration

NB: You should avoid checking in the ".env" file to VCS, the .env.local should be OK to share, but it is best practice to not bundle production credentials with your VCS.

Astronomer answered 4/4, 2018 at 14:4 Comment(4)
I am using different database for testing (PHPUnit) and developing currently. I require to use php artisan tinker for testing database. Do I need to change my env file separately for tests as well as for development? Yes, your answer too helped me.Stingo
I would suggest using different environments, yes. This is by no means anything you have to follow.Astronomer
Unfortunately, regardless of whether I specify an env, I can't get tinker to honor the log settings in .env, and nothing ever appears in my log, even when there are errors.Steelworks
@Ryan, sorry for being late to the party here, but for future curious people: that is most likely due to a file permission error.Astronomer
G
6

To set the default database connection to 'mysql_test' from within tinker I use this command:

>>> use DB
>>> DB::setDefaultConnection('mysql_test');

It is especially useful when you want to test your migrations and seeders without messing up your existing (working) local database.

Graf answered 31/7, 2019 at 15:56 Comment(0)
P
1

Change default connection

$model_instance = new App\YourModel();
$model_instance->setConnection('new_connection');
$data = $model_instance->find(1);
Peahen answered 4/4, 2018 at 11:40 Comment(6)
No. it did not help.Stingo
@DonRaider try updated answer please and let me knowPeahen
Thank you. It did help. One more question. Can I change my default connection for php artisan tinker to testing instead of mysql database connection?Stingo
yes, you can. Just remember if you can change default connection in your controller code then you can do that in tinker too, its similarPeahen
This is not a good answer as it requires extra code to control the connection based on context (in tinker, in unit tests, etc.), whereas the better answer by @Kristian Hareland only requires a command line flag and a config file.Holzman
@Holzman i think so too. My answer only provide a solution not the best one.Peahen

© 2022 - 2024 — McMap. All rights reserved.