How to make SQLite work in Laravel

混江龙づ霸主 提交于 2019-11-30 08:39:12

Short Solution

Though not answering the question, the way to fix "Database not found" issue is to replace the following line in database.php:

'database' => env('DB_DATABASE', database_path('database.sqlite')),

with

'database' => database_path('database.sqlite'),

The .env file should contain this:

DB_DATABASE=..\database\database.sqlite

With some testing you can verify that the link included in DB_DATABASE is relative to the 'public' directory(at least on my windows machine). That's why we should introduce ..\ before your link.

And of course, using an absolute link should do it too

DB_DATABASE=D:\www\project\database\database.sqlite

as @Josh suggests

The .env file should contain this:

   DB_DATABASE=..\database\database.sqlite

With some testing you can verify that the link included in DB_DATABASE is relative to the 'public' directory(at least on my windows machine). That's why we should introduce ..\ before your link.

And of course, using an absolute link should do it too

   DB_DATABASE=D:\www\project\database\database.sqlite 

as @Josh suggests

Update

By using the relative path, migrations will fail because they use project directory as root directory...

To correct everything, I suggest setting:

   DB_DATABASE=database\database.sqlite

and tweaking the sqlite connections in config/database.php as follows:

  'database' => env('DB_DATABASE/..', database_path('database.sqlite')),

Complementing the awnser by our friend @alexander-lomia

Change:

'database' => env('DB_DATABASE', database_path('database.sqlite'))

To:

'database' => database_path(env('DB_DATABASE'))

in database.php

:)

Instead of a relative path you need to use an absolute path in your .env file.

DB_DATABASE=/var/www/project/database/database.sqlite

or in your case:

DB_DATABASE=D:\www\project\database\database.sqlite

Shortest and easiest solution is to remove default mysql settings in .env and work in database.php. That's what worked for me at least.

Remove the following...

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

I got the same problem as you did. You have to use the absolute path in ENV file.

Please check the official documentation about this https://laravel.com/docs/5.4/database

DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database.sqlite

This worked for me in Laravel 5.5

  1. In .env file just have the connection name and remove all other DB_ related settings: DB_CONNECTION=sqlite_testing

  2. Define your sqlite settings in the config/database.php file:

    'connections' => [
    
        'sqlite_testing' => [
            'driver' => 'sqlite',
            'database' => database_path('testing-db.sqlite'),
            'prefix' => '',
        ],
    
        ...
    ]
    

My file is in the database/testing-db.sqlite.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!