Whenever I work on a new branch, I find it useful to have a unique table for the branch and for my testing purposes.
Lets assume we have three tables denoted in our `.env` file like that:

 
    DB_CONNECTION      = mysql_local 
    DB_DATABASE        = mastertable
    DB_DATABASE_BRANCH = branchtable
    DB_DATABASE_TEST   = testtable

then switch to your new branch and change


       'database' => env('DB_DATABASE', 'forge'),

in config/database.php to DB_DATABASE_BRANCH, it should look like this:


    'connections' => [
    
           //....
    
          'mysql_local' => [
                'driver' => 'mysql',
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE_BRANCH', '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' => 'InnoDB',
            ],

Since config/database.php is tracked by git, you will automatically switch your database when using git checkout ….

When you migrate your branch into master, make sure to reset ‘database’ => env(‘DB_DATABASE_BRANCH’, ‘forge’), back to ‘database’ => env(‘DB_DATABASE’, ‘forge’),`. To make sure that forgetting this is not affecting your production server, I would recommend to use DB_CONNECTION=mysql in the .env of your production server and DB_CONNECTION=mysql_local for your local workstation.

Another thing that I find super useful, is to use an extra clean table for my testing purposes. The table that I use in my branch is often just a copy from my production server. For testing purposes, its good to have a clean table which is refreshed after each test (make sure not to re-migrate, because that is time consuming). To do this, just create a new connection driver for your testing table in config/database.php :


    'connections' => [
    
           //....
    
      'testing' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_TEST', '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' => 'InnoDB',
        ],

inside your phpunit.xml specifiy that driver:


   
           //....

 <php>
       <env name="APP_ENV" value="testing"/>
       <env name="CACHE_DRIVER" value="array"/>
       <env name="SESSION_DRIVER" value="array"/>
       <env name="QUEUE_DRIVER" value="sync"/>
       <env name="DB_CONNECTION" value="testing"/>
   </php>
</phpunit>

If you want to migrate your testing database you can call the migrate command like this:


php artisan migrate --database=testing
adam