udoyen udoyen - 4 months ago 99
PHP Question

laravel 5.2 sqlite connection error

I have this situation in laravel 5.2 where if I do this:

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=
DB_DATABASE=database/database.sqlite
DB_USERNAME=
DB_PASSWORD=


in the
.env
file I get this:


InvalidArgumentException in SQLiteConnector.php line 34:
Database (database/database.sqlite) does not exist.


on trying to manipulate the
laravel
error rendering system with this code from a controller:

<?php

namespace App\Http\Controllers;

use App\User;

class SampleController extends Controller
{
public function findUser()
{
$user = User::firstOrFail();

return $user->toArray();
}
}


And this goes well with
php artisan migrate
command, but to get the
controller
code to render the error as expected I have to do this:

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=
DB_DATABASE=../database/database.sqlite
DB_USERNAME=
DB_PASSWORD=


To fix the problem so I get both
php artisan migrate
and
SampleController
to work I have put this in the
config/database.php
file:

'connections' => [

'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],


Why is the
default
means to access
sqlite
as given on the laravel website not working. Is it a bug I should be aware of?

Answer

The normal configuration in the config.php file is expected to be

'sqlite' => [
        'driver'   => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix'   => '',
    ],

Furthermore, the appropriate way to mention the DB_DATABASE in the .env file is to specify the absolute file name, such as /var/www/laravel/database/database.sqlite not the relative location.

For example, in Windows

DB_DATABASE=C:\wamp\www\laravel\database\database.sqlite

And this is what is mentioned in the Laravel Documentation

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