Introducing the PHP, Laravel & Doctrine DBAL client for libSQL

Cover image for Introducing the PHP, Laravel & Doctrine DBAL client for libSQL

PHP is the most used server-side programming language on the web. It's estimated that over 75% of the web is using PHP, so it makes sense that Turso extends its range of libSQL clients to enable PHP developers to build zero network latency applications with SQLite.

We're excited to announce that you can now use libSQL with PHP, Doctrine DBAL, and Laravel as an extension.

The new native libSQL PHP extension supports local, remote, embedded replicas, with support for periodic sync, read your writes, encryption at rest and prepared statements, as well as batch and interactive transactions.

Once you’ve installed the extension (see GitHub for latest releases), and configured it, you can LibSQL class:

<?php

$db = new LibSQL("file.db");

$db->query("SELECT * FROM users", ())->fetchArray();

#Working locally with SQLite and PHP

As shown in the example above, you can pass the filename for a SQLite file to the LibSQL constructor to instantiate a client that uses your local disk for development:

<?php

$db = new LibSQL("file.db");

#Embedded Replicas

Once you’re ready to go to production, you can keep the local filename for a local replica, but now provide the syncUrl which will update the local SQLite file with a replica of your remote database — provided by Turso.

Take advantage of zero-latency reads by embedding your database right alongside your PHP application.

<?php

$config = [
    "url" => "file:replica.db",
    "syncUrl" => getenv('TURSO_DATABASE_URL'),
    "authToken" => getenv('TURSO_AUTH_TOKEN')
];

$db = new LibSQL($config);

You can control the timing for synchronization between remote and local by providing the syncInterval value (in seconds):

<?php

$config = [
    "url" => "file:replica.db",
    "syncUrl" => getenv('TURSO_DATABASE_URL'),
    "authToken" => getenv('TURSO_AUTH_TOKEN'),
    "syncInterval" => 10
];

$db = new LibSQL($config);

Embedded Replicas require access to the file system, so if you’re working in environments that don’t support access (like serverless), you can use Turso’s remote connection string to instantiate your database client:

<?php

$db = new LibSQL("libsql:dbname=libsql://[databaseName]-[organizationName].turso.io;authToken=...");

#Turso also now supports Laravel

The new turso-driver-laravel package enables Laravel developers to use libSQL as their database, remote or embedded, powered by turso-client-php native extension for PHP.

You can configure how you intend to use Turso inside your Laravel config/database.php:

'libsql' => [
    'driver' => 'libsql',
    'url' => 'file:' . env('DB_DATABASE', database_path('database.db')),
    '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' => '',
],

Now you can use the libSQL driver to talk to your Turso database using the same APIs and methods you’re used to:

<?php

use Illuminate\Support\Facades\DB;

// Create
DB::table('users')->create([
    'name' => ‘Iku’,
    'email' => ‘iku@turso.tech’
]);

// Read
DB::table('users')->get();

// Update
DB::table('users')
    ->where('id', 1)
    ->update(['name' => 'The real']);

// Delete
DB::table('users')
    ->where('id', 1)
    ->delete();

A huge thanks to Iman Ali Mustofa for taking the lead of bringing together ideas from the Turso Discord Community to create a single PHP driver that enables developers to build apps with PHP, Laravel, and more.

You can get started by following the official PHP Quickstart, PHP Reference, and examples on GitHub.

scarf