PHP code example of vuthaihoc / laravel-xtdb2

1. Go to this page and download the library: Download vuthaihoc/laravel-xtdb2 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/ */

    

vuthaihoc / laravel-xtdb2 example snippets


// config/database.php
'xtdb' => [
    'driver' => 'xtdb',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'xtdb'),
    'username' => env('DB_USERNAME', 'xtdb'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'sslmode' => 'disable',
    // 'dates_from_strings' => false,   // see "Values and types"
],

use Illuminate\Database\Eloquent\Model;
use LaravelXtdb\Eloquent\Casts\AsDocument;
use LaravelXtdb\Eloquent\HasXtdbKey;

class User extends Model
{
    use HasXtdbKey;

    protected function casts(): array
    {
        return [
            'active' => 'boolean',
            'settings' => AsDocument::class,   // a queryable nested document
        ];
    }
}

User::create(['name' => 'Alice', 'settings' => ['theme' => 'dark', 'tags' => ['a']]]);
User::where('settings->theme', 'dark')->whereJsonContains('settings->tags', 'a')->get();

// Writes for a period of valid time
DB::table('prices')->validFrom('2026-01-01', '2026-07-01')->insert(['_id' => 'p1', 'price' => 10]);
DB::table('prices')->validFrom('2026-07-01')->insert(['_id' => 'p1', 'price' => 12]);            // open-ended
DB::table('prices')->validFrom('2026-03-01', '2026-04-01')->where('_id', 'p1')->update(['price' => 11]);
DB::table('prices')->validFrom('2026-05-01', '2026-06-01')->where('_id', 'p1')->delete();          // gap in May
DB::table('prices')->forAllValidTime()->where('_id', 'p1')->update(['price' => 9]);                // whole history

// Reads
DB::table('prices')->asOfValidTime('2026-03-15')->where('_id', 'p1')->value('price');           // 11
DB::table('prices')->validBetween('2026-03-01', '2026-06-01')->get();                             // valid at some point
DB::table('prices')->where('_id', 'p1')->history()->get();                                        // every version, with _valid_from/_valid_to
DB::table('prices')->asOfSystemTime(now()->subDay())->asOfValidTime('2026-03-15')->get();        // as known yesterday
DB::table('prices')->forAllValidTime()->forAllSystemTime()->get();                                // everything ever recorded

// Removal
DB::table('prices')->where('_id', 'p1')->delete();   // ends the validity now; the history stays
DB::table('prices')->where('_id', 'p1')->erase();    // removes the row and its whole history

use LaravelXtdb\Eloquent\Bitemporal;
use LaravelXtdb\Eloquent\HasXtdbKey;

class Price extends Model
{
    use Bitemporal, HasXtdbKey;
}

Price::asOfValidTime('2026-03-15')->find('p1');
$price->price = 13;
$price->saveValidFrom('2026-09-01');            // valid from September, unbounded
$price->saveValidFrom('2026-03-01', '2026-04-01');   // a correction for March only
$price->versions();                             // every version, with _valid_from / _valid_to
$price->deleteValidFrom('2026-01-01', '2026-02-01');
$price->erase();                                // the row and its history are gone

Schema::create('users', function (Blueprint $table) {
    $table->ulid('_id')->primary();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamps();
    $table->softDeletes();
});
// create table "users" ("_id", "name", "email", "created_at", "updated_at", "deleted_at")

$user = User::where('email', $email)->firstOrFail();   // read first

DB::transaction(function () use ($user) {              // then write
    $user->update(['name' => 'Alice']);
    Post::create(['user_id' => $user->getKey(), 'title' => 'Hello']);
});

Order::query()->asOfTime(now()->subHour())->sum('total');   // the totals as they were an hour ago
Word::suggest('word', $search)->limit(10)->get();