How to make SQLite work in Laravel - sqlite

Whenever I run php artisan migrate, the following error is shown in the console:
[PDOException]
SQLSTATE[HY000] [14] unable to open database file
The database.sqlite file is located at database/. I'm running Windows 10, Laravel 5.2. Here is .env file config:
.env:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=homestead
DB_PASSWORD=secret
I have looked everywhere, but could not find what causes this error and how to resolve it.
Update
I managed to make migrations run successfully by replacing DB_DATABASE=database with DB_DATABASE=database/database.sqlite in .env file. However, new error occurs whenever I try to retrieve items from the database:
public function index()
{
// cause of the error
$cards = Card::all();
return view('cards.index', compact('cards'));
}
The above action throws following error:
InvalidArgumentException in SQLiteConnector.php line 34:
Database (database/database.sqlite) does not exist.
The strange thing is, that the command Card::all() works flawlessly in php artisan tinker mode. What kind of magic is that?
Anyway, I've also found out, that the line:
'database' => env('DB_DATABASE', database_path('database.sqlite')),
in database.php file needs to be replaced with just database_path('database.sqlite') and everything starts to work normally.
It seems, that the root of the problem is env('DB_DATABASE') call. I went to SQLiteConnector.php file and dumped the output of both env('DB_DATABASE') and database_path('database.sqlite'). Here are their outputs respectively:
dd(env('DB_DATABASE')) // => 'database/database.sqlite'
dd(database_path('database.sqlite')) // => 'D:\www\project\database\database.sqlite'
As you see, their output differs, and the second one is what is expected. Is this a Laravel bug? Or did I misunderstood something?

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'),

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')),
Original Answer
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

Create file called database.sqlite in this folder as database/database.sqlite
Open the .env file and change MySQL to SQLite
Comment password and username and databaseName using '#'
run php artisan migrate enjoy
env file like this:
DB_CONNECTION=sqlite
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=database
#DB_USERNAME=homestead
#DB_PASSWORD=secret

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

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
In .env file just have the connection name and remove all other DB_ related settings: DB_CONNECTION=sqlite_testing
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.

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 found the following works rather well, if you prefer to use relative paths:
replace the 'database' line under SQLite with the following:
'database' => database_path(env('DB_DATABASE', 'database.sqlite')),

This was the setting I had used,
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
I also had to run touch database.sqlite in the database folder.

The problem is that php artisan migrate will use the DB_DATABASE from the root of the project, while serving the project with php artisan serve will run from the public directory.
The best solution for me is to create a link to database inside public directory:
UNIX: ln -s database public/database
Windows: mklink /J public\database database
Of course then you would have to deny access to database folder from HTTP requests but that should be easily done.

Related

Create Drupal 8 Multisite on localhost

I want to create a multisite Drupal 8 site. I would like to keep the default site, and then add another site. This is all being done on a WAMP stack and is on a locahost. These are the steps I have taken, but have not worked:
Downloaded most recent Drupal 8 and unzipped (and moved files) to www/my_primary_site
I created two databases in MySQL: site1, site2
in the www/my_primary_site/sites folder, I did the following:
a. Created a folder called "my_second_site"
b. Created a file called sites.php.
c. In sites.php, I have this code:
$sites = array(
'8080.localhost.my_primary_site' => 'default',
'8080.localhost.my_primary_site/my_second_site' => 'my_second_site',
);
I then go to localhost/my_primary_site. This brings me to the Drupal install (localhost/my_primary_site/core/install.php). But, if I go to localhost/my_primary_site/my_second_site, it just redirects me back to localhost/my_primary_site/core/install.php.
I would expect that the second link would take me to a different install path. Is this correct? If not, how would I fix this?
thanks
jason
Make sure to copy the default settings.php and services.yml to the new folder.
Try using the format of using . instead of /' like this:
$sites = array(
'8080.localhost.my_primary_site' => 'default',
'8080.localhost.my_primary_site.my_second_site' => 'my_second_site',
);
Make a symbolic link in your root directory that points to the main Drupal folder.
ln -s /drupal /my_primary_site/my_second_site
Try using this:
$sites['site1.localhost'] = 'site1';
$sites['site2.localhost'] = 'site2';
Also, make sure you add this domain to your etc/hosts file:
127.0.0.1 site1.localhost
127.0.0.1 site2.localhost
There are two approaches :
Try using sysmlinks functions in file myfile.php
2.Using sites.php in /drupal/sites folder
$sites['site1'] = 'site1';
$sites['site2'] = 'site2';

How to set up SQLITE for laravel webserver?

I want to use SQLITE for my laravel5 project on my webserver. 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
when I do
php artisan migrate
it says there is no database
I think what I wrote as the path for the database in the database.php is wrong and do I may need to somehow write where my ip adress is in the .env file? I have been googling all night and can't seem to figure this out.
You dont need to edit the .php files at all. You can all do it in the .env file, since the files in the config directory are written to first use the values from the .env file, and if they are not defined, fall back on what is defined there.
env('DB_CONNECTION','mysql') would yield the value from the .env file, and only if it is not defined fall back to mysql.
So in your .env file just put the following:
DB_CONNECTION=sqlite
DB_DATABASE=database/database.sqlite
and create a file called database.sqlite in your database directory (thats where its supposed to be by convention.). That's it.
For default laravel 5.2+ installation:
create an sqlite database file
$ cd storage
$ touch database.sqlite
$ cd ..
make it writeable
$ chmod -R 777 storage
at ".env" file:
DB_CONNECTION=sqlite
DB_DATABASE=storage/database.sqlite
and remove or comment all other DB_* records
If you prefer to use relative path, instead of absolute to database file
at "config/database.php"
instead of:
'database' => env('DB_DATABASE', database_path('database.sqlite')),
write:
'database' => __DIR__ . '/../' . env( 'DB_DATABASE' ),
now, laravel app will be able to find sqlite file, and php artisan will work too
Why are you using HTTP link? I guess it should link to a .sqlite DB file:
'database' => __DIR__.'/../database/production.sqlite',
http://laravel-recipes.com/recipes/118/setting-up-the-sqlite-database-driver

The KnpSnappyBundle Is Not Working

I am trying my best to understand why the KnpSnappyBundle does not work at all. Inside of Symfony2.
This is the error I keep getting time and time again:
Warning: file_put_contents(/var/folders/l7/_w4sky2d457czb5v3d0133y40000gn/T/knp_snappy52eb2be7bf7915.98143786.html): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/symfonydev/vendor/knplabs/knp-snappy/src/Knp/Snappy/AbstractGenerator.php line 330
This is line 330:
file_put_contents($filename, $content);
I have done everything to the "T" with exactly how it explains how to install it.
I have installed WKHTMLTOPDF using HomeBrew and it now resides in my /usr/local/bin directory.
My config.yml file looks like this:
# Knp Snappy Configuration
knp_snappy:
pdf:
enabled: true
binary: "wkhtmltopdf"
options: []
My code looks like this:
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml("hello world"),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="'.$pdfTimeStamp.'.pdf"'
)
);
I can generate a pdf from command line when calling this code:
/usr/local/bin/wkhtmltopdf http://www.google.com/ /User/xxxxx/Desktop/thepdf.pdf
And yet I get this error everytime.
PLEEEEEASE HELP! Thanks so much!
Please check if your shell user has write permissions for /var/folders/l7/_w4sky2d457czb5v3d0133y40000gn/T/.
try to use these binary path /usr/local/bin/wkhtmltopdf instead of using the wkhtmltopdf.

Getting install.php when export site from development server to production

I've copied files and imported DB dump to MySQL on production server but when I try to access website settings.php file loads. I've got settings.php file in my site subdirectory in /sites folder. Here's it's content:
$databases = array('default' => array('default' => array(
'driver' => 'mysql',
'database' => 'dmitry_dkkb',
'username' => 'root',
'password' => 'fg67klbn0',
'host' => '127.0.0.1')));
I guess i just missed something when i edited this file
Try setting the drive to mysqli. Also, follow this steps:
1) Take a back up of your db and save it to a safe place
2) Remove all the tables from the db, and remove the setting.php or rename it
3) Do you clean install of Drupal using those files
4) Once finished, import the db back up
This process always works on all platforms, in all the situation

How do I get Drush rsync to use site-aliases correctly?

I'm using Drush 4.2 and I'm trying to rsync files from a the dev server to my local machine. My aliases.drushrc.php is located in the root of my local drupal installation and has the following in it:
$aliases['local'] = array(
'root' => '/Users/christian/Sites/site-root',
'path-aliases' => array(
'%files' => 'sites/default/files'
),
);
$aliases['dev'] = array(
'root' => '/var/www/vhosts/some-domain.com/subdomains/dev/httpdocs',
'remote-host' => 'some-domain.com',
'remote-user' => 'root',
'path-aliases' => array(
'%drush' => ' /var/tools/drush/drush',
'%files' => 'sites/default/files',
),
);
As a test I try to run this from the local drupal root:
drush rsync #dev:%files ~/Desktop/test/
I expect #dev:%files to expand to the remote file path but instead I get:
You will destroy data from /Volumes/MacintoshHD/Users/christian/Desktop/test/ and replace with data from #dev:/Volumes/MacintoshHD/Users/christian/Sites/site-root/%files
Any ideas?
UPDATE:
I've also found that when I'm try the command:
drush dd #dev:%files
I get
Target '#dev:%files' not found.
UPDATE 2
I've found that the issue seems to be coming from the location of the aliases.drushrc.php file. I had it in the drupal root of the site I was working on. I found that if I moved it to ~/.drush/ then everything worked perfectly.
I'd prefer to have it under source control though. I tried putting it in sites/default/ but it had the same problems as before. I'll award the bounty to whomever tells me where to put this file so it's under the source controlled site root.
You can set the alias path in your drushrc.php config file.
If this is not set, then drush searches these locations (in this order) for the alias file.
/etc/drush
drush installation folder
$HOME/.drush
insides the sites folder of your drupal site
Check the readme for more details.
BTW: You have a comma missing in your declaration of 'local' alias.
Modify it as:
'%files' => 'sites/default/files',

Resources