Table of contents
In this post, I will share how to use SQLite Database in Laravel 8, and 9. 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 you an example. Just follow my few steps below on how to implement the Laravel SQLite database.
Â
First, Install Laravel 9.
Point to your htdocs directory and open your command prompt. Then run the following command:
composer create-project laravel/laravel laravel-sqlite
Â
Once installed run the next command to your command prompt.
cd laravel-sqlite
Â
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=
Â
Now, let's change it with the following lines below.
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
Â
As you can see we changed the DB_CONNECTION
value to SQLite
and removed DB_DATABASE
, DB_USERNAME
, and DB_PASSWORD
.
Â
Third, Create Laravel SQLite Database
Now lets, create our database inside your Laravel 9 directory project and navigate to the following path: project_folder/database
then create a file named database.sqlite
.
Â
Â
Fourth, Run Migration Command
Now, let's run the migration command.
php artisan migrate
Â
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);
});
Â
Then run the command:
php artisan serve
Â
Then run this to your browser: http://127.0.0.1:8000/
Â
That's it you have successfully installed Laravel SQLite to your local server on windows. I hope it helps.
Â
Cheers :)
Read next