Setting custom path for sqlite db in Laravel - php

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

Related

Django postgres database not connecting with Lumen

I am trying to connect django.db.backends.postgresql with my lumen application. But when I run the query then following error occured
could not find driver
Even I change the driver type from mysql to pgsql
My database connection
'ml_db' => [
'driver' => 'django.db.backends.postgresql',
'host' => env('ML_DB_HOST'),
'port' => env('ML_DB_PORT'),
'database' => env('ML_DB_NAME'),
'username' => env('ML_DB_USER'),
'password' => env('ML_DB_PASS'),
'charset' => env('DB_CHARSET', 'utf8mb4'),
'collation' => env('DB_COLLATION', 'utf8mb4_general_ci'),
'prefix' => '',
'timezone' => env('DB_TIMEZONE', '+00:00'),
'strict' => false,
],
Is there any way to connect this database with my lumen application because I am using multiple databases in application.
The obvious answer here is, that you haven't installed the corresponding driver for your used dbms. So just install it.
from your configuration posted in the question, I assume that it's your lumen configuration. There is no 'django.db.backends.postgresql' driver in lumen. What we have is pgsql driver unless you create such a custom driver yourself,(which I assume is not the case).
so change the driver parameter to the following?
'driver' => 'pgsql',
Lumen .env file:
DB_CONNECTION=pgsql
DB_HOST=your host
DB_PORT=5432
DB_DATABASE=database
DB_USERNAME=username
DB_PASSWORD=password
The link

Laravel phpunit drops database tables

I'm trying to test my app using phpunit with laravel but each time I run
unit test
vendor\bin\phpunit
All tables in my database drop so I have to migrate each time I run a test
Have you encountered with similar issue & how can I fix it ?
Use a separate connection for tests. Add this connection to config/database.php config file:
'connections' => [
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],

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.

Laravel 5 + PostgreSQL: "Database [postgres] not configured." Error

I'm trying to get started with Laravel + PostgreSQL and been following the database tutorial.
Unfortunately, after updating the database configuration file and running php artisan migrate, the following error appears:
[InvalidArgumentException]
Database [postgres] not configured.
What puzzles me is that I didn't specify the "postgres" database in the configuration, but another database I set through cPanel, say "example_database".
Here's some relevant parts of my /config/database.php configuration:
'default' => env('DB_CONNECTION', 'postgres')
And inside the connections array of the same file:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'example_database'), // This seems to be ignored
'username' => env('DB_USERNAME', 'example_username'),
'password' => env('DB_PASSWORD', 'example_password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public'
],
The actual database credentials I'm using are working perfectly on my SQL Workbench client, so this seems to be a Laravel config problem. Any ideas? I have searched around for at least an hour to no avail.
You have to enter your configuration in the .env file.
The configuration you made will only be loaded if they are not already defined in .env
You need to use pgsql instead of postgres.
DB_CONNECTION=pgsql
DB_HOST=localhost
DB_DATABASE=DB_NAME
DB_USERNAME=USER
DB_PASSWORD=PW
Laravel sometimes caches your configurations. If you run into this problem while everything looks alright try running
php artisan config:cache
I know this is an old question and it already has an answer; however, here is an small explanation why:
If you check your database.php in the config directory, you will see that you have few connections types, including pgsql. So, the key have to match to the DB_CONNECTION in .env file. You can definitely replace pqsql connection key with postgres, and it will work on the same way.
However, I would recommend replacing the value DB_CONNECTION, instead of modifying the config.
DB_CONNECTION=pgsql

Laravel PHPUnit PDOException exception

I don't know why when I run a PHPUnit test, I get the following error:
PDOException: SQLSTATE[HY000] [2002] No such file or directory
My testing environment database setting is:
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'clg_test',
'username' => 'root',
'password' => 'veryHardPass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]
],
'migrations' => 'migrations',
];
The reason why I am using MySQL as oppose to SQLite is because my migration files use dropColumn which is not supported by SQLite. I call the migration via Artisan::call('migrate') in the setup.
If I actually call migration manually in the terminal via php artisan migrate --env=testing then the migration IS successful and the databases are created.
Why am I then facing the above problem?
Try changing localhost to 127.0.0.1. The message you're getting is indicative of the script not being able to connect to MySQL through a socket, but using an ip should work.

Categories