Turso + Laravel
Set up Turso in your Laravel project in minutes
Prerequisites
Before you start, make sure you:
- Install the Turso CLI
- Sign up or login to Turso
- Have an Laravel Application
Download the latest build extension/driver binary you can see at Release page. It's available for:
- Linux
- Mac/Darwin
- Windows (WSL) / Devcontainer
Now, LibSQL
class is available in your PHP environment! You can use it everywhare in your PHP project 🎉
Configure database credentials
Get the database URL:
turso db show --url <database-name>
Get the database authentication token:
turso db tokens create <database-name>
Assign credentials to the environment variables inside .env
.
DB_SYNC_URL=
DB_AUTH_TOKEN=
Create Laravel Project
Using Composer:
composer create-project laravel/laravel turso-laravel
If you already have laravel installer:
laravel new turso-laravel
Install Turso Driver Laravel
Install turso-driver-laravel
:
composer require tursodatabase/turso-driver-laravel
Add this LibSQLServiceProvider
class at bootstrap/providers.php
:
Turso\Driver\Laravel\LibSQLServiceProvider::class
Configure database connection
Setup the environment variable in your Laravel application, choose what the type connection you need.
Other environment variables (Embedded Replica Only)
Database configuration
Add this configuration at config/database.php
inside the connections
array:
'libsql' => [
'driver' => 'libsql',
'url' => 'file:' . env('DB_DATABASE', 'database.sqlite'),
'authToken' => env('DB_AUTH_TOKEN', ''),
'syncUrl' => env('DB_SYNC_URL', ''),
'syncInterval' => env('DB_SYNC_INTERVAL', 5),
'read_your_writes' => env('DB_READ_YOUR_WRITES', true),
'encryptionKey' => env('DB_ENCRYPTION_KEY', ''),
'remoteOnly' => env('DB_REMOTE_ONLY', false),
'database' => null,
'prefix' => '',
],
Create database seeders
In fresh Laravel installation, your can open file database/seeders/DatabaseSeeder.php
and change the code with this:
<?php
namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
// We will create 10 fake users
User::factory(10)->create();
}
}
then create the database seeders:
php artisan db:seed --class=DatabaseSeeder
Execute Query
Examples
Was this page helpful?