Version 2 of the PHP SDK gives you two ways to query SurrealDB: raw SurrealQL through run(), and fluent query builders such as select(), create(), update(), and delete(). The builders compile to the same parameter-bound queries you could write by hand, so you can mix the two freely.
API references
| Method | Description |
|---|---|
$db->run($surql, $bindings) | Executes raw SurrealQL statements |
$db->select($target) | Selects records from the database |
$db->create($target) | Creates a new record |
$db->insert($target, $data) | Inserts one or many records |
$db->update($target) | Updates existing records |
$db->delete($target) | Deletes records from the database |
$db->relate($from, $edge, $to) | Creates graph relationships between records |
Running raw SurrealQL
The run() method executes raw SurrealQL statements. Pass bindings as the second argument to inject values safely. It returns one result per statement.
For a multi-statement query, each result keeps its position in the returned list.
Builders, execute, and compile
Every builder method returns a builder object. Call execute() to run it and get the result of the single statement, or compile() to get the BoundQuery without running it.
Selecting records
The select() method reads records. Pass a Table to read all records, or a RecordId to read one. Chain fields(), where(), start(), limit(), and fetch() to refine the query.
The where() method accepts a SurrealQL string for static conditions. For dynamic values, pass a BoundQuery so the values stay parameterised.
Creating records
The create() method starts a CREATE. Chain content() to set the record data. A Table generates a random ID; a RecordId creates the record with that ID.
Inserting records
The insert() method inserts one or many records in a single statement. Pass a target table and the records, or pass the records alone when each contains its own ID.
Chain relation() to insert into a relation table (INSERT RELATION), or ignore() to skip records that already exist (INSERT IGNORE).
Updating records
The update() and upsert() methods modify records. Choose a strategy by chaining content(), merge(), replace(), or patch().
Replace the record with new data. Fields not included are removed.
You can filter which records to update with where().
Deleting records
The delete() method removes records. It defaults to RETURN BEFORE, so the deleted records are returned.
Creating graph relationships
The relate() method creates edges in SurrealDB's graph model. Pass the source, the edge table, and the target, with optional edge data.
Running functions
The call() method invokes a SurrealQL or SurrealML function by name. Pass an optional version and a list of arguments.
Setting session parameters
Use let() to define a parameter on the session and unset() to remove it. Session parameters are available in later queries as $name.
Learn more
Query Builders API reference for every builder method
Live queries for real-time subscriptions
Transactions for atomic multi-statement operations
SurrealQL statements for the query language reference