Skip to content

surrealdb/surrealdb.php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

d3482e4 · Mar 31, 2025

History

27 Commits
Mar 31, 2025
May 13, 2024
Mar 31, 2025
Mar 31, 2025
May 13, 2024
Mar 24, 2023
Jul 20, 2024
Mar 24, 2023
Nov 5, 2024
Mar 24, 2023
Mar 28, 2025
Mar 28, 2025

 

The official SurrealDB SDK for PHP.


     

     

surrealdb.php

The official SurrealDB SDK for PHP.

Documentation

View the SDK documentation here.

How to install

You install the SurrealDB SDK via Composer. If you don't have Composer installed, you can download it here.

composer require surrealdb/surrealdb.php

Getting started

To get started, you need to create a new instance of the SurrealDB HTTP or WebSocket Class.

// Make a new instance of the SurrealDB class. Use the ws or wss protocol for having WebSocket functionality.
$db = new \Surreal\Surreal();

$db->connect("http://localhost:8000", [
    "namespace" => "test",
    "database" => "test"
]);

Basic Querying

In the PHP SDK, We have a simple API that allows you to interact with SurrealDB. The following example shows how to interact with the database.

The example below requires SurrealDB to be installed and running on port 8000.

// Connect set the specified namespace and database.
$db = new \Surreal\Surreal();

$db->connect("http://localhost:8000", [
    "namespace" => "test",
    "database" => "test"
]);

// We want to authenticate as a root user.
$token = $db->signin([
    "user" => "root",
    "pass" => "root"
]);

// Create a new person in the database with a custom id.
$person = $db->create("person", [
    "title" => "Founder & CEO",
    "name" => [
        "first" => "Tobie",
        "last" => "Morgan Hitchcock" 
    ],
    "marketing" => true
]); 

// Get the person with the name "John Doe".
$record = \Surreal\Cbor\Types\Record\RecordId::create("person", "john");
$person = $db->select($record);

// Update a person record with a specific id
$record = \Surreal\Cbor\Types\Record\RecordId::create("person", "john");
$person = $db->merge($record, ["age" => 31]);

// Select all people records.
$people = $db->select("person");  

// Perform a custom advanced query.
$groups = $db->query('SELECT marketing, count() FROM $tb GROUP BY marketing', [
    "tb" => \Surreal\Cbor\Types\Table::create("person")
]);

// Close the connection between the application and the database.
$db->close();

Contributing

Requirements

  • PHP 8.1 or higher
  • Composer
  • SurrealDB 1.4.0 or higher

Run tests

./vendor/bin/phpunit -c phpunit.xml

Directory Structure

  • src - The source code of the library
  • tests - The unit tests of the library