# 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.

## Model queries

Define [Surqlize models](/docs/reference/php/libraries/surqlize/models.md) and query them directly. They run through the Laravel-managed connection.

```php
$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](/docs/reference/php/libraries/surqlize/querying.md) and [Mutations](/docs/reference/php/libraries/surqlize/mutations.md) for the full model API.

## Raw SDK access

Resolve the SDK client for lower-level access.

```php
use SurrealDB\SDK\Surreal;

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

## The manager facade

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

```php
use SurrealDB\Laravel\Facades\SurrealDB;

SurrealDB::health();

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

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

## Transactions

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.

```php
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.

## Learn more

- [Container and facades](/docs/reference/php/frameworks/laravel/container-and-facades.md) for the facade method reference
- [Surqlize transactions](/docs/reference/php/libraries/surqlize/transactions.md) for the batching mechanism
- [Testing](/docs/reference/php/frameworks/laravel/testing.md) for asserting the queries your code sends
