• Start

Frameworks

/

Laravel

Queries and transactions

Run Surqlize model queries, raw SurrealQL, and transactions in a Laravel application, including against named connections.

In Laravel you query SurrealDB three ways: through Surqlize models, through the raw SDK client, and through the SurrealDB manager facade. The integration registers the executor, so model queries work without passing one.

Define Surqlize models and query them directly. They run through the Laravel-managed connection.

$users = User::select(fn ($user) => [$user->id, $user->name])
    ->where(fn ($user) => $user->name->eq('beau'))
    ->collectModels();

$user = User::create(['name' => 'Tobie', 'age' => 32], id: 'tobie');

See Querying and Mutations for the full model API.

Resolve the SDK client for lower-level access.

use SurrealDB\SDK\Surreal;

$result = app(Surreal::class)->run('RETURN $message', ['message' => 'hello']);

The SurrealDB facade runs SurrealQL with lifecycle helpers around the same client, and targets a named connection with the connection: argument.

use SurrealDB\Laravel\Facades\SurrealDB;

SurrealDB::health();

$result = SurrealDB::run('RETURN $message', ['message' => 'hello']);

$analytics = SurrealDB::run(
    'RETURN $message',
    ['message' => 'hello'],
    connection: 'analytics',
);

The Surqlize facade runs a transaction over the executor. Run each query inside the callback through the transaction it passes you, with the executor: argument or withExecutor(). The transaction commits when the callback returns and rolls back if it throws.

use SurrealDB\Laravel\Facades\Surqlize;

Surqlize::transaction(function ($transaction): void {
    User::createQuery(['name' => 'beau'], executor: $transaction)->execute();
});

Surqlize::transaction(
    fn ($transaction) => User::createQuery(['name' => 'beau'], executor: $transaction)->execute(),
    connection: 'analytics',
);
Note

Surqlize::transaction() uses Surqlize's executor-based SurrealQL transaction batching. SDK-native transaction ids are not exposed through the Laravel facade yet, because they depend on the WebSocket transport and server feature support.

Was this page helpful?