• Start

Languages

/

PHP

/

v2 (alpha)

/

API Reference

Query builders

Reference for the fluent query builders in version 2 of the PHP SDK, including select, create, update, delete, insert, and relate.

The fluent builders compile a statement and run it through the connection. Each builder method on Surreal returns a builder object you configure with chained calls, then run with execute() or inspect with compile().

All builders extend QueryBuilder in the SurrealDB\SDK\Query namespace.

Every builder provides these methods.

MethodReturnsDescription
execute()mixedRun the statement and return the first statement's result
compile()BoundQueryCompile to SurrealQL and bindings without running
json(bool $json = true)staticRequest JSON-compatible results

Execute raw SurrealQL with optional bindings. Returns one result per statement.

Syntax

$db->run(string $surql, array $bindings = []): array
[$people] = $db->run('SELECT * FROM person WHERE age > $min', ['min' => 18]);

Execute a pre-built BoundQuery. Returns one result per statement.

Syntax

$db->query(BoundQuery $query): array

Start a SELECT. Accepts a RecordId, Table, or string target.

Methods

->fields(string ...$fields)   // SELECT specific fields
->value(string $field) // SELECT VALUE for one field
->where(string|BoundQuery $cond)
->start(int $start)
->limit(int $limit)
->fetch(string ...$fields) // resolve record links
->timeout(string $duration) // e.g. "5s"
->version(string $datetime) // historical read
$people = $db->select(new Table('person'))
->fields('name', 'age')
->where('age >= 18')
->limit(10)
->execute();

Start a CREATE. Accepts a RecordId, Table, or string target.

Methods

->content(array|object $data)       // CONTENT
->patch(array $patches) // PATCH
->output(Output $output) // RETURN clause
->timeout(string $duration)
->version(string $datetime)
$person = $db->create(new RecordId('person', 'tobie'))
->content(['name' => 'Tobie'])
->execute();

Start an UPDATE or UPSERT. update() modifies existing records; upsert() creates the record if it does not exist.

Methods

->content(array|object $data)   // replace the record
->merge(array|object $data) // merge fields
->replace(array|object $data) // REPLACE
->patch(array $patches) // JSON Patch
->where(string|BoundQuery $cond)
->output(Output $output)
->timeout(string $duration)
$db->update(new RecordId('person', 'tobie'))
->merge(['age' => 33])
->execute();

$db->upsert(new RecordId('person', 'tobie'))
->content(['name' => 'Tobie', 'age' => 33])
->execute();

Start a DELETE. It defaults to RETURN BEFORE, so deleted records are returned.

Methods

->output(Output $output)
->timeout(string $duration)
->version(string $datetime)
$db->delete(new RecordId('person', 'tobie'))->execute();

Start an INSERT. Pass a target table and records, or records alone when each carries its own ID.

Methods

->relation()   // INSERT RELATION
->ignore() // INSERT IGNORE
->output(Output $output)
->timeout(string $duration)
->version(string $datetime)
$db->insert(new Table('person'), [
['name' => 'Alice'],
['name' => 'Bob'],
])->execute();

Start a RELATE, creating one or more graph edges.

Methods

->content(array|object $data)   // store data on the edge
->unique()
->output(Output $output)
->timeout(string $duration)
->version(string $datetime)
$db->relate(
new RecordId('person', 'tobie'),
new Table('likes'),
new RecordId('post', 'surrealdb'),
)->content(['since' => 2024])->execute();

Invoke a SurrealQL or SurrealML function by name. run() already handles raw SurrealQL, so function invocation has its own method.

Syntax

$db->call(string $name, ?string $version = null, array $args = []): RunQuery
$greeting = $db->call('fn::greet', null, ['Tobie'])->execute();

Compile to SELECT * FROM ONLY $auth, returning the authenticated record user.

$me = $db->auth()->execute();

output() accepts the SurrealDB\SDK\Enum\Output enum: NONE, NULL_, DIFF, BEFORE, AFTER.

use SurrealDB\SDK\Enum\Output;

$db->update(new RecordId('person', 'tobie'))
->merge(['age' => 33])
->output(Output::AFTER)
->execute();

where() accepts a SurrealQL string or a BoundQuery fragment. Use a BoundQuery to keep dynamic values parameterised.

timeout() accepts a SurrealQL duration string such as 5s or 1m30s.

Was this page helpful?