PHP code example of ntanduy / cloudflare-d1-database
1. Go to this page and download the library: Download ntanduy/cloudflare-d1-database library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ntanduy / cloudflare-d1-database example snippets
use App\Models\Post;
// Create
$post = Post::create([
'title' => 'Hello from D1',
'body' => 'This is stored in Cloudflare D1!',
]);
// Read
$posts = Post::where('published', true)->orderBy('created_at', 'desc')->get();
// Update
$post->update(['title' => 'Updated Title']);
// Delete
$post->delete();
use Illuminate\Support\Facades\DB;
// Select
$users = DB::connection('d1')->table('users')
->where('active', true)
->limit(10)
->get();
// Insert
DB::connection('d1')->table('users')->insert([
'name' => 'John Doe',
'email' => '[email protected]',
]);
// Raw queries
$results = DB::connection('d1')->select('SELECT * FROM users WHERE id = ?', [1]);
use Ntanduy\CFD1\D1\D1Connection;
/** @var D1Connection $connection */
$connection = DB::connection('d1');
$connection->getDriver(); // 'rest' or 'worker'
$connection->isWorkerDriver(); // true or false
use Ntanduy\CFD1\D1\D1Connection;
/** @var D1Connection $connection */
$connection = DB::connection('d1');
$results = $connection->batch([
['sql' => 'SELECT * FROM users WHERE id = ?', 'params' => [1]],
['sql' => 'UPDATE stats SET views = views + 1 WHERE id = ?', 'params' => [5]],
['sql' => 'SELECT COUNT(*) as total FROM posts'],
]);
$user = $results[0]; // Result set from first statement
$stats = $results[1]; // Result set from second statement
$count = $results[2]; // Result set from third statement
use Ntanduy\CFD1\D1\D1Connection;
/** @var D1Connection $connection */
$connection = DB::connection('d1');
// Start a session — first query goes to any instance (fastest)
$connection->withSession('first-unconstrained');
// Or start with the latest data from primary
$connection->withSession('first-primary');
// Execute queries — bookmarks are tracked automatically
$users = DB::table('users')->get();
$posts = DB::table('posts')->get();
// Get the current bookmark (for passing to another request/session)
$bookmark = $connection->getBookmark();
// Start a new session from a previous bookmark
$connection->withSession($bookmark);
// End the session when done
$connection->endSession();
use Ntanduy\CFD1\D1\Exceptions\CircuitBreakerOpenException;
try {
$users = DB::connection('d1')->table('users')->get();
} catch (CircuitBreakerOpenException $e) {
// Circuit is open — fail fast, use fallback or return cached data
}
bash
# Get the current bookmark
php artisan d1:time-travel
# Get the bookmark at a specific timestamp
php artisan d1:time-travel --timestamp="2024-01-15T10:30:00+00:00"
# Unix timestamps also work
php artisan d1:time-travel --timestamp=1705312200
bash
php artisan d1:schema-dump
bash
# Schema only (no data)
php artisan d1:schema-dump --no-data
# Custom output path
php artisan d1:schema-dump --path=./backup.sql
# Delete migration files after dumping (same as native schema:dump --prune)
php artisan d1:schema-dump --prune
# Specify connection name
php artisan d1:schema-dump --connection=d1
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.