PHP code example of adaiasmagdiel / loxodontu-php

1. Go to this page and download the library: Download adaiasmagdiel/loxodontu-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adaiasmagdiel / loxodontu-php example snippets


use AdaiasMagdiel\Loxodontu\Client;

$loxo = new Client(
    'https://your-app.example.com/api/v1',
    'my-project', // project id / slug
    'PROJECT_API_KEY',
);

// A chain ends with an explicit ->get() (PHP has no thenable to auto-execute on await).
$response = $loxo->from('todos')
    ->select()
    ->eq('done', false)
    ->order('created_at', ascending: false)
    ->limit(20)
    ->get();

$todos = $response->data;

$loxo->from('todos')->insert(['title' => 'Write docs'])->get();
$loxo->from('todos')->update(['id' => 1, 'done' => true])->get(); // single row, id in the body
$loxo->from('todos')->update(['done' => true])->eq('done', false)->get(); // bulk, by filter
$loxo->from('todos')->delete()->eq('id', 1)->get(); // single row, by filter
$loxo->from('todos')->delete()->lt('views', 10)->get(); // bulk, by filter
$loxo->from('todos')->delete([2, 3, 4])->get(); // bulk, by id list

// End users (your app's own users, separate from your platform account)
$loxo->auth->register('[email protected]', 'password123');
$loxo->auth->login('[email protected]', 'password123'); // token stored & sent automatically
$loxo->auth->logout();

// Edge functions
$response = $loxo->functions->invoke('daily-cleanup', body: ['source' => 'client']);

use AdaiasMagdiel\Loxodontu\Admin;

$admin = new Admin('https://your-app.example.com/api/v1');
$admin->auth->login('[email protected]', 'password123');

$projects = $admin->projects->list()->unwrap();
$project = $admin->projects->create(['name' => 'New project'])->unwrap();

$project1 = $admin->projects->for($project['id']);
$project1->tables->create([
    'name' => 'todos',
    'columns' => [
        ['name' => 'title', 'type' => 'text'],
        ['name' => 'done', 'type' => 'boolean', 'default_value' => false],
    ],
]);
$project1->keys->create(['name' => 'frontend', 'permissions' => ['select', 'insert']]);
$project1->tables->rlsPolicies($tableId)->create([
    'name' => 'owner can read',
    'operation' => 'SELECT',
    'conditions' => ['user_id' => '$auth.id'],
]);
$project1->sql('SELECT COUNT(*) FROM todos');

final class LoxodontuResponse
{
    public readonly mixed $data;
    public readonly ?array $error; // ['message' => string, 'status' => int]
    public readonly ?int $count;   // from X-Total-Count on paginated list endpoints
    public readonly int $status;
}

use AdaiasMagdiel\Loxodontu\LoxodontuError;

try {
    $todos = $loxo->from('todos')->select()->get()->unwrap();
} catch (LoxodontuError $e) {
    // $e->getMessage(), $e->status()
}

use AdaiasMagdiel\Loxodontu\SessionStorage;

$loxo = new Client($url, $projectId, $apiKey, ['storage' => new SessionStorage()]);
bash
composer