Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel + SQLite = SQLSTATE[HY000]General Error: 8 attempt to write a readonly database

I'm trying to delete a db.sqlite file, create it again and then insert some info into this DB all in the same method. This is the method I'm using:

public function destroy()
{
    // Store all contents and delete the first one since this is created via seeder
    $contents = $this->contents->all()->toArray();
    array_shift($contents);

    // Delete db file, creates it from an example file and changes permissions
    system('rm -rf ../database/database.sqlite');
    system('cp ../database/database.sqlite.example ../database/database.sqlite');
    system('chmod 0777 ../database/');
    system('chmod 0777 ../database/database.sqlite');

    // Insert data
    foreach ($contents as $content) {
        $this->contents->create($content);
    }

    return response()->json(['message' => 'Data has been destroyed!']);
}

However when I try to run it i get the error described in the title:

SQLSTATE[HY000]General Error: 8 attempt to write a readonly database

I'm setting 777 permissions on the file and the folder as recommended by others questions' answers, so I have no idea why it isn't working.

like image 417
miguelopezv Avatar asked Dec 15 '17 15:12

miguelopezv


3 Answers

You need to run two commands

First, change ownership of the Laravel directory to web group:

sudo chown -R :www-data /var/www/yourLarvelFolder

Second, give privileges over storage directory so it can be writable:

sudo chmod -R 775 /var/www/yourLarvelFolder/storage

like image 141
Zymawy Avatar answered Nov 18 '22 17:11

Zymawy


give permission here in ubuntu working on 18.04 and laravel 6

sudo chmod -R 775 database
sudo chown -R $(whoami) database
like image 4
Jignesh Joisar Avatar answered Nov 18 '22 16:11

Jignesh Joisar


If you are on Fedora or Redhat with Selinux rememebr to switch off Selinux (or configure it correctly):

setenforce 0
like image 2
gdm Avatar answered Nov 18 '22 17:11

gdm