laravel and phpunit: could not find driver (SQL: PRAGMA foreign_keys = ON;)

48.9k views Asked by At

I have run my laravel app with phpunit. Everything is fine until at some point when I run my test again turns out with this error.

Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)

enter image description here

Caused by
PDOException: could not find driver

Here's my phpunit.xml file:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
     colors="true">
<testsuites>
    <testsuite name="Unit">
        <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
        <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
</testsuites>
<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app</directory>
    </whitelist>
</filter>
<php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="DB_CONNECTION" value="sqlite"/>
    <server name="DB_DATABASE" value=":memory:"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
</php>
OS: Windows 10 | php version: PHP 7.4.11 | sqlite version: SQLite version 3.19.1

Does anyone encounter with this error in laravel 6 and phpunit?

12

There are 12 answers

3
Vic Andam On BEST ANSWER

This is actually happening after I upgraded my php version in laragon. The solution is to enable pdo_sqlite on php extension in laragon. That's it. This answer found here https://laracasts.com/discuss/channels/general-discussion/phpunit-sqlite-error-after-updating-laragon-pdoexception-could-not-find-driver

0
Raihan Taher On

just enabling these two extension from php.ini file worked for me.

extension=pdo_sqlite

extension=sqlite3
1
asiye yaghubi On

If you use Ubuntu try this:

php --version
sudo apt-get install PHP (your php version)-sqlite3

This works for me.

https://laracasts.com/discuss/channels/laravel/sqlite-database-throw-error-could-not-find-driver-sql-pragma-foreign-keys-on

0
Patrick  Wambua On

If you come across such an error,simply do the following;

1.run this command to install sqlite driver in your ubuntu or linux OS

sudo apt-get install php-sqlite3
  1. You also need to enable pdo_sqlite extension in your php.ini too, check the path of your loaded .ini with the below command

    php --ini

3.Then check and verify that the pdo_sqlite extension is loaded.

php -m | grep pdo_sqlite

Then the error will be solved.

1
Emmanuel-Odero On

For those using windows;

Navigate to the php.ini file(-C:\php\php.ini)

Enable the following:

;extension=pdo_sqlite by removing the /;/ should look like this extension=pdo_sqlite

;extension=sqlite3 should be extension=sqlite3 without the ; symbol
0
Sliq On

PHP 8.1

sudo apt install php8.1-sqlite3

Note that it needs the 8.1 part!

0
Mohammadreza Khalilikhorram On

At first uncomment ;extension=pdo_sqlite in php.ini in /etc/php/{your_php_version}/php.ini on Linux or C:\php\php.ini on Windows by removing the semicolon at the beginning.

then sudo service apache2 restart

then sudo apt-get install php-sqlite3

0
Jignesh Joisar On

You need to install sqlite3 package in linux distro

sudo apt-get install php$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')-sqlite3  php-sqlite3

This command automatically install the sqlite3 package based on your current PHP version in your Linux distro.

0
Nesar Ahmad Nori On

In case if you have multiple versions of PHP in Laragon you will still see the error. The solution is to enable pdo_sqlite extension on all versions. The reason is that Laragon has its own PHP but Laravel uses the system PHP and you might not know which one is system PHP version.

0
Hasibur Rahman On

This happens because PDO Sqlite is not enabled. to enable it in Laragon, just open Laragon click on menu > PHP > Extensions > pdo_sqlite That's it.

And if you don't use Laragon the solution might be different. Thanks

0
Saif On

Just do the following changes on the php.ini file

extension_dir = "<php installation directory>/php-7.4.3/ext"
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
sqlite3.extension_dir = "<php installation directory>/php-7.4.3/ext"
2
Eahiya On

If You face this problem

(could not find driver (SQL: PRAGMA foreign_keys = ON;))

You can simply run bellow command on your Ubuntu system

sudo apt-get install php-sqlite3

Also if you want to install specific version like php 8.1 simply run bellow command

sudo apt install php8.1-sqlite3

You might need to enable pdo_sqlite extension in your php.ini too, you can get the path of your loaded ini with the below command:

php --ini

simply add extension=pdo_sqlite to your php.ini and verify it is loaded by running

php -m | grep pdo_sqlite

I think it will help you.. Thanks