Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 : Use different database for testing and local

Tags:

php

laravel

How does one change database for development and testing on local system without editing the .env file each time?

I have found it quite inconvenient to practice TDD because of this.

Is it possible for a Laravel application to differentiate between normal development and testing so that it can choose the appropriate database?

like image 335
James Okpe George Avatar asked Feb 05 '16 15:02

James Okpe George


People also ask

Which DB to use with Laravel?

Currently, Laravel provides first-party support for five databases: MariaDB 10.3+ (Version Policy) MySQL 5.7+ (Version Policy) PostgreSQL 10.0+ (Version Policy)

How many databases are supported by Laravel?

Currently Laravel supports four database systems: MySQL, Postgres, SQLite, and SQL Server.

How does Laravel connect to external database?

In . env file you can set DB_CONNECTION with your database name and applicable databases are given in /config/database. php which are (SQLite, MySQL, pgSQL, SQLSRV) after that just type your username, password, and database name and you can use that database with port number.


1 Answers

Create a testing database configuration in Laravel

Edit the config\database.php file and add a testing - array into the connections array:

'connections' => [     'testing' => [         'driver' => env('DB_TEST_DRIVER'),         // more details on your testing database     ] ] 

Then add the necessary variables to your .env-file.

Edit PHPUnit configuration

Open your phpunit.xml-file and add the following within your <php>-tag:

<env name="DB_CONNECTION" value="testing"/>

Now PHPUnit will run with the tests on the database you defined in the testing - array.

like image 121
Tijmen Avatar answered Sep 23 '22 17:09

Tijmen