DEV Community

Cover image for How To Use Laravel 8 SQLite Database using Windows
Code And Deploy
Code And Deploy

Posted on

How To Use Laravel 8 SQLite Database using Windows

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-use-sqlite-database-in-laravel-8-using-windows

In this post, I will share how to use SQLite Database in Laravel 8. If your project is light and not a huge database then SQLite is suitable to use. It is an option also to use SQLite while developing locally. Now I will show it to you with an example. Just follow my few steps below on how to implement the Laravel SQLite database.

First, Install Laravel 8.

Point to your htdocs directory and open your command prompt. Then run the following command:

composer create-project laravel/laravel laravel-sqlite
Enter fullscreen mode Exit fullscreen mode

Once installed run the next command to your command prompt.

cd laravel-sqlite
Enter fullscreen mode Exit fullscreen mode

Second, Setup ENV for our SQLite Database

By default, you will see these lines inside the .env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Enter fullscreen mode Exit fullscreen mode

Now, let's change it with the following lines below.

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
Enter fullscreen mode Exit fullscreen mode

As you can see we changed the DB_CONNECTION value to SQLite and removed DB_DATABASE, DB_USERNAME, DB_PASSWORD.

Third, Create Laravel SQLite Database

Now lets, create our database inside your Laravel 8 directory project navigate to the following path: "project_folder/database" then create a file named "database.sqlite".

how-to-use-sqlite-database-in-laravel-8-using-windows

Fourth, Run Migration Command

Now, let's run the migration command.

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Once done your Laravel SQLite database is ready.

Now let's check application is working and if successfully connected with the SQLite database.

See below code from routes/web.php.

<?php

use App\Models\User;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {

    User::updateOrCreate([
        'email' => 'jdoe@gmail.com'
    ],[
        'name' => 'John Doe',
        'email' => 'jdoe@gmail.com',
        'password' => bcrypt('password')
    ]);


    $users = User::all();

    print_r($users);
});
Enter fullscreen mode Exit fullscreen mode

Then run the command:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Then run this to your browser: http://127.0.0.1:8000/

how-to-use-sqlite-database-in-laravel-8-using-windows

That's it you have successfully installed Laravel SQLite to your local server on windows. I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-use-sqlite-database-in-laravel-8-using-windows if you want to download this code.

Happy coding :)

Top comments (0)