PHP code example of zymawy / dgraph

1. Go to this page and download the library: Download zymawy/dgraph 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/ */

    

zymawy / dgraph example snippets


return [
];
 
use Zymawy\Dgraph\DgraphClient;

/** @var DgraphClient $client */
$client = new DgraphClient('http://localhost:8080');


use Zymawy\Dgraph\Api\Operation;
use Zymawy\Dgraph\Exceptions\DgraphException;
use Zymawy\Dgraph\Types\StringType;
use Zymawy\Dgraph\Types\IntType;
use Zymawy\Dgraph\DgraphClient;

/** @var DgraphClient $client */
$client = new DgraphClient('http://localhost:8080');

// Define the schema using the Operation class
$operation = new Operation();
$operation
  ->addField("name", new StringType(["index(term)"]))
  ->addField("age", new IntType(["index(int)"]))
  ->addType("person", ["name", "age"]);

try {
    $response = $client->alter($operation);
    if ($response->hasErrors()) {
        throw new DgraphException("Schema alteration error: " . json_encode($response->getErrors()));
    } else {
        echo "Schema successfully altered.";
    }
} catch (DgraphException $e) {
    echo "Error: " . $e->getMessage();
}
 
use Zymawy\Dgraph\DgraphClient;
use Zymawy\Dgraph\Txn;
use Zymawy\Dgraph\Responses\DgraphResponse;
use Zymawy\Dgraph\Exceptions\DgraphException;
use Zymawy\Dgraph\Types\StringType;
use Zymawy\Dgraph\Types\IntType;
/** @var DgraphClient $client */
$client = new DgraphClient("http://localhost:8080");

use Zymawy\Dgraph\Api\Mutation;
use Zymawy\Dgraph\Exceptions\DgraphException;

/** @var DgraphClient $client */
$client = new DgraphClient("http://localhost:8080");

$mutation = new Mutation();
$mutation->set([
  [
    "uid" => "_:account1",
    "name" => "Hamza",
    "balance" => 1000.0
  ],
  [
    "uid" => "_:account2",
    "name" => "Zymawy",
    "balance" => 500.0
  ]
]);

try {
  $response = $client->mutate($mutation, true); // commitNow set to true
  if ($response->hasErrors()) {
    throw new DgraphException(
      "Mutation error: " . json_encode($response->getErrors())
    );
  } else {
    print_r($response->getData());
  }
} catch (DgraphException $e) {
  echo "Error: " . $e->getMessage();
}

bash
php artisan vendor:publish --tag="dgraph-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="dgraph-config"
bash
php artisan vendor:publish --tag="dgraph-views"