laravel 5.2 sqlite connection error - php

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?

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

Related

Setting custom path for sqlite db in Laravel

I want to use the sqlite db stored in "C:\Folder\my_database.sqlite" , what should i do? I want to use that sqlite db. Can anyone please suggest me a solution?
So i can give the path to sqlite in database.php below:
'sqlite' => [
'driver' => 'sqlite',
'database' => "path to my sqlite db",
'prefix' => '',
],
Also if that is done, then i can change my default connection to sqlite. Like:
DB::setDefaultConnection('sqlite');
Can anyone please suggest me a solution?
As the Laravel Documentation states:
After creating a new SQLite database using a command such as touch
database/database.sqlite, you can easily configure your environment
variables to point to this newly created database by using the
database's absolute path:
'sqlite' => [
'driver' => 'sqlite',
'database' => "C:\Folder\my_database.sqlite",
'prefix' => '',
],
To make it the default connection, just set this in your .env file:
DB_CONNECTION=sqlite
Open your .env file and set,
DB_CONNECTION=sqlite
DB_DATABASE=C:\Folder\my_database.sqlite
further more information you can see: https://laravel.com/docs/5.7/database
for set sqlite database you can add absoulate path like this in database.php file..
'sqlite' => [
'driver' => 'sqlite',
'database' => 'C:\Folder\my_database.sqlite',
'prefix' => '',
],
for set a default sqlite connection update this in your .env file:
DB_CONNECTION=sqlite
for production update line(number 16) in database.php
'default' => env('DB_CONNECTION', 'sqlite'),
and then run this command in terminal for remove old configration and create new configration file.
php artisan config:cache

Laravel seems to randomly use the default database.php configuration instead of .env ones

EDIT : Thanks to #PedroFaria99, clear config cache solved the problem, but if anybody want to bring an explanation about the randomness aspect feel free.
I have an issue with my laravel 5.5 local installation (production environnement isn't impacted). Here Laravel is used as an API and serves a client-sided VueJS application.
Sometimes (randomly), my laravel is returning 500 error to my client. It can happens on various routes, never the same one, after 1 to 10 successives HTTP request or not and when I check the storage
[2018-03-09 13:44:08] production.ERROR: PDOException: SQLSTATE[HY000] [1045] Access Denied for user: 'forge'#'#localhost' (password: NO) in [...] Illuminate\Database\Connectors\Connector.php:119
However, my .env file is settup, and my database.php is using env() with default parameters are "forge" and "localhost". So I tried to change this parameter to "test", and the next 500 errors was same but with "test" instead of "forge".
I'm very confused, since this error doesn't happen systematicly.
.env file
APP_ENV=local
APP_DEBUG=true
DB_HOST=localhost
DB_DATABASE=mydatabase
DB_USERNAME=root
DB_PASSWORD=
database.php
...
'default' => env('DB_CONNECTION', 'mysql'),
...
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'test'),
'password' => env('DB_PASSWORD', '')
...
Try
php artisan cache:clear
php artisan config:cache
It happens because laravel saves env values in its cache and if your config is not cached it will take the configuaration that are saved in the database.php file. You can not directly access env values on runtime.
I hate to revive a dead thread but after some soul searching I found the issue might just be php's lack of thread safety : https://github.com/laravel/framework/issues/28571 and the great writeup here : https://mattallan.me/posts/how-php-environment-variables-actually-work/

Dusk SQLite database not found

I'm getting this error:
Database (homestead) does not exist.
When I try this simple Dusk test:
class ShowArticleTest extends DuskTestCase
{
use DatabaseMigrations;
/** #test */
public function it_shows_all_articles()
{
Article::create([
'title' => 'My First Article',
'body' => 'Some body text.'
]);
$this->browse(function (Browser $browser) {
$browser->visit('/articles')
->assertSee('My First Article');
});
}
}
I can see in the stack trace that the error comes from the controller method that handles the /articles request, which looks like this:
public function all()
{
Article::all();
}
So it seems that the database is accessible by the test itself but not by the controller.
The .env.dusk.local file looks like this:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
The homestead file is being created in my project root folder. The file is found inside the virtual machine too and the permissions looks like this:
-rw-rw-r-- 1 vagrant vagrant
Tried setting DATABASE to database/testing.sqlite in .env.dusk.local. The file is created once Dusk starts but the error still says that it can't find the database/testing.sqlite database.
Database (database/testing.sqlite) does not exist.
The database file can be accessed using the sqlite3 CLI and I can query for records without problem.
I'm using Laravel 5.5 and Homestead.
The problem occurs inside config/database.php, around here:
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
]
Because DB_DATABASE inside .env.dusk.local is defined as homestead, the configuration value for database.connections.sqlite.database ends up just the same: homestead.
The actual value should be the full path to the file, which can be obtained with the database_path() helper. So, by simple moving database_path() out of the env() call:
'database' => database_path(env('DB_DATABASE', 'database.sqlite'))
The value for database.connections.sqlite.database becomes the full path to the database file and everything seems to be working.

Database doesn't exist when php artisan migrate

I migrated my local laravel 5 project onto my AWS ec2 lamp Ubuntu server.
http://realtoraxe.com/realtoraxe/public/ but it shows
InvalidArgumentException in SQLiteConnector.php line 34: Database does not exist.
I changed the database.php set for sqlite
<?php
return array(
'default' => 'sqlite',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => 'http://realtoraxe.com/realtoraxe/storage/database.sqlite',
'prefix' => '',
),
),
);
?>
and I changed the .env to
APP_ENV=local
APP_DEBUG=true
APP_KEY=mystring
DB_CONNECTION=sqlite
CACHE_DRIVER=file
SESSION_DRIVER=file
it still doesn't see the database
Laravel probably does not support much of the helper functions( e.g. storage_path) inside config files, but it surely does supports env function. More info here
That's the reason it's unable to locate your database. You should define the database in .env file or use the full path in the config file instead.

"Access denied for user ''#'localhost' to database 'forge" appears randomly

I have a search function for my database but sometimes I get this message:
[2016-02-04 07:03:18] local.ERROR: PDOException: SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge' in C:\xampp\htdocs\reko\api\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:55
Stack trace:
#0 C:\xampp\htdocs\reko\api\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(55): PDO->__construct('mysql:host=loca...', 'forge', '', Array)
...
In one of ten calls I get this 500 error message, but I don't know why. The other calls give the right result.
.env
APP_ENV=local
APP_DEBUG=true
APP_KEY=bJM6O0MnrIPaTNwKKOqNJkGinRDv1fnc
DB_HOST=localhost
DB_DATABASE=reko
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
Search function:
public function search(Modul $modul, Request $request)
{
$question = Question::whereModulId($modul->id)
->where('value', 'LIKE', '%' . $request->get('keywords') . '%')
->with('tags')
->whereHas('exams', function ($query) use ($request) {
$query->where('date', '>=', $request->get('year').'-01-01');
});
if (!$request->get('parent'))
$question->where('type', '<>', 'parent');
if (!$request->get('own'))
$question->where('type', '<>', 'own');
if (!$request->get('normal'))
$question->where('type', '<>', 'normal');
if ($request->get('answered'))
$question->has('answers');
return $question->paginate(10);
}
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I haven't modified the database.php file and all other calls work great.
PLEASE RUN THIS COMMAND
php artisan cache:clear
THEN
php artisan config:cache
This seems to hold most of the answers : Laravel 5.2 not reading env file
Remember to not to edit your core files as one of the users said.
Just do the safe checks, clear cache, should work.
Hope it helps.
As a workaround you can add to your DB forge account with all privileges.
Try php artisan clear:cache to clear your cache and re-configure your cache php artisan config:cache it might works for you .
I had the same issue once. what I discovered was that my default connection was set to PostgreSQL instead of MySQL so I changed my default connection in database.php
'default' => env('DB_CONNECTION', 'mysql'),
other wise do add following to your .env
DB_CONNECTION=mysql
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=
try adding remaining methods in your .env file
Too often we have to face this error when hosting.
You should see that you have given Privileged Users permission to the database in the hosting.
First
First I need to see if the database name is correct.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=zhara_demo
DB_USERNAME=zhara_admin
DB_PASSWORD=zhara#2021
or
Try php artisan clear:cache to clear your cache and re-configure
your cache php artisan config:cache it might works for you .

Categories