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?

Waqleh
  • 9,741
  • 8
  • 65
  • 103
Lizesh Shakya
  • 2,482
  • 2
  • 18
  • 42

3 Answers3

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.

MrK
  • 1,060
  • 9
  • 23
  • 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. – Lizesh Shakya Apr 05 '18 at 04:23
  • I would suggest using different environments, yes. This is by no means anything you _have_ to follow. – MrK Apr 09 '18 at 11:44
  • 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. – Ryan Feb 18 '19 at 18:24
  • @Ryan, sorry for being late to the party here, but for future curious people: that is most likely due to a file permission error. – MrK Nov 10 '21 at 00:23
5

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.

windsor
  • 379
  • 4
  • 9
1

Change default connection

$model_instance = new App\YourModel();
$model_instance->setConnection('new_connection');
$data = $model_instance->find(1);
Sohel0415
  • 9,523
  • 21
  • 30
  • No. it did not help. – Lizesh Shakya Apr 04 '18 at 12:34
  • @DonRaider try updated answer please and let me know – Sohel0415 Apr 04 '18 at 12:41
  • 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? – Lizesh Shakya Apr 05 '18 at 04:10
  • yes, you can. Just remember if you can change default connection in your controller code then you can do that in tinker too, its similar – Sohel0415 Apr 05 '18 at 05:05
  • 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. – Soulriser Jul 19 '19 at 15:36
  • 1
    @Soulriser i think so too. My answer only provide a solution not the best one. – Sohel0415 Jul 22 '19 at 04:43