ali-eltaweel/neo4j-query-builder

There is no license information available for the latest version (1.0.0) of this package.

Neo4j Query Builder for PHP

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/ali-eltaweel/neo4j-query-builder

1.0.0 2025-11-30 00:00 UTC

This package is auto-updated.

Last update: 2025-11-30 00:03:48 UTC


README

Neo4j Query Builder for PHP

Installation

Install neo4j-query-builder via Composer:

composer require ali-eltaweel/neo4j-query-builder

Usage

Creating Nodes

Here is an example of how to create a node with the label PERSON and properties name and age:

$q = new Neo4jQueryBuilder\QueryBuilder();

$q->create()->node()->alias('n')->label('PERSON')->properties([ 'name' => 'Alice', 'age' => 30 ]);

echo $q;

The previous code snippet will generate the following Cypher query:

CREATE (n:PERSON { name: "Alice", age: 30 })

Creating multiple nodes in a single query can be done as follows:

$q = new Neo4jQueryBuilder\QueryBuilder();

$q->create(function(Neo4jQueryBuilder\Clauses\Create $create) {
  
  $create->node()->alias('russell')->label('PLAYER')->properties([ 'name' => 'Russell Westbrook', 'age' => 33 ]);
  $create->node()->alias('lebron')->label('PLAYER')->properties([ 'name' => 'LeBron James', 'age' => 36 ]);
});

The above code will generate the following Cypher query:

CREATE (russell:PLAYER { name: "Russell Westbrook", age: 33 }), (lebron:PLAYER { name: "LeBron James", age: 36 })

Creating Relationships

The following example demonstrates how to create a relationship between two nodes:

$q = new Neo4jQueryBuilder\QueryBuilder();

$q->create(function(Neo4jQueryBuilder\Clauses\Create $create) {

  $create->relationship(function(Neo4jQueryBuilder\RelationshipBuilder $relationship) {

    $relationship->from()->alias('russell');
    $relationship->to()->alias('lebron');

    $relationship->label('TEAMMATE_OF')->property('since', 2021);
  });
});

The above code will generate the following Cypher query:

CREATE (russell)-[:TEAMMATE_OF { since: 2021 }]->(lebron)

Creating Nodes and Relationships Together

$q = new Neo4jQueryBuilder\QueryBuilder();

$q->create(function(Neo4jQueryBuilder\Clauses\Create $create) {

  $create->relationship(function(Neo4jQueryBuilder\RelationshipBuilder $relationship) {

    $relationship->from()->alias('russell')->label('PLAYER')->properties([ 'name' => 'Russell Westbrook', 'age' => 33 ]);
    $relationship->to()->alias('lebron')->label('PLAYER')->properties([ 'name' => 'LeBron James', 'age' => 36 ]);
    
    $relationship->label('TEAMMATE_OF')->property('since', 2021);
  });
});
CREATE (russell:PLAYER { name: "Russell Westbrook", age: 33 })-[:TEAMMATE_OF { since: 2021 }]->(lebron:PLAYER { name: "LeBron James", age: 36 })

Matching Nodes

$q->match()->node()->alias('n');
MATCH (n)

Matching Relationships

$q->match()->relationship(function(RelationshipBuilder $r) {

  $r->alias('r')->label('TEAMMATE_OF');
});
MATCH ()-[r:TEAMMATE_OF]->()

Deleting Elements

$q->delete()->detach()->element('n');
DETACH DELETE n

Filtering Nodes and Relationships

$q->match()->node()->alias('player')->label('PLAYER');
$q->where()->condition()->name('id(player)')->operator('=')->value(10);
MATCH (player:PLAYER)
WHERE id(player) = 10
$q->match()->relationship()->alias('rel')->label('TEAMMATE_OF');
$q->where()->condition()->name('rel.since')->operator('=')->value(2021);
MATCH ()-[rel:TEAMMATE_OF]->()
WHERE rel.since = 2021

Returning Elements

$q->match()->node()->alias('n');
$q->return()->element('n');
MATCH (n)
RETURN n

Updating Elements

$q->match()->node()->alias('n');
$q->set()->expression('n.active = true');
$q->return()->element('n');
MATCH (n)
SET n.active = true
RETURN n
$q->match()->node()->alias('n');
$q->remove()->expression('n:REF');
$q->return()->element('n');
MATCH (n)
REMOVE n:REF
RETURN n