• Start

Languages

/

PHP

/

Libraries

/

Surqlize (ORM)

Mutations

Create, update, upsert, and delete records in Surqlize with model helpers and the mutation builder, including payload modes and return modes.

Surqlize exposes mutations two ways. Model helpers such as create() and save() cover the common cases and return hydrated models. The mutation builder gives full control over the payload, the return clause, and conditional updates.

create() inserts a record and returns the model. Pass an id to set a specific record id.

$user = User::create(['name' => 'beau', 'age' => 27]);

$user = User::create(['name' => 'beau', 'age' => 27], id: 'beau');

save() creates the record when the model has no RecordId, and updates it when it does.

$user = User::findOrFail('beau');
$user->age = 28;
$user = $user->save();

upsert() creates the record if it does not exist, or updates it if it does. It requires an id.

$user = User::upsert(['name' => 'beau', 'age' => 27], id: 'beau');

delete() removes the record the model points at.

$user = User::findOrFail('beau');
$user->delete();

The read helpers return models or scalars.

MethodPurpose
all()Fetch every record as models
find($id)Find one model by id, or null
findOrFail($id)Find one model by id, or throw ModelNotFoundException
count($where?)Count records, optionally filtered
exists($where?)Whether at least one matching record exists
refresh()Reload the model instance from the database
$count = User::count(fn ($user) => $user->age->gte(18));

if (User::exists(fn ($user) => $user->name->eq('beau'))) {
// ...
}

For more control, build the mutation explicitly. createQuery() returns a builder instead of running immediately, and updateWhere() and deleteWhere() target records by predicate.

$query = User::createQuery(['name' => 'beau', 'age' => 27], id: 'beau');
$query->compile();
// CREATE user:beau CONTENT {"name":"beau","age":27} RETURN AFTER

Update or delete records that match a condition.

User::updateWhere(fn ($user) => $user->age->gte(18))
->merge(['verified' => true])
->returnAfter()
->execute();

User::deleteWhere(fn ($user) => $user->age->lt(13))
->returnBefore()
->execute();
MethodSurrealQL
content($data)CONTENT
merge($data)MERGE
replace($data)REPLACE
patch($patches)PATCH
MethodSurrealQL
returnNone()RETURN NONE
returnBefore()RETURN BEFORE
returnAfter()RETURN AFTER
returnDiff()RETURN DIFF
returning($fields)Return selected fields
returningValue($field)Return one selected value

timeout($amount, $unit = 's') adds a statement timeout. To run the mutation, call one of:

MethodResult
execute()The raw SDK result
executeModels()A list of hydrated models
firstModel()The first hydrated model, or null
compile()A literal SurrealQL string
toBoundQuery()An SDK BoundQuery
$user = User::createQuery(['name' => 'beau'])
->returnAfter()
->firstModel();

Was this page helpful?