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/ */
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