A simple interface to build neo4j cypher queries programmatically

v1.0 2020-05-26 15:50 UTC

This package is auto-updated.

Last update: 2023-01-22 01:44:10 UTC


This package is still a work in progress, what I have tested works so far but should not be used in a production environment yet

The purpose of this package was to create a fluent interface for building Cypher queries to use with a neo4j Graph Database. This package is not designed to actually run the queries for you, I am developing another package for that currently.


Using composer (when published)

$ composer require juststeveking/cypher-query-builder

You are then free to use it as needed within your projects.


Using this library is very simple, you start a cypher query and using the available clauses programmatically build up your cypher query string.



require __DIR__ . '/vendor/autoload.php';

$query = Cypher::query()
    ->match('Person', 'person')
    ->where('person', 'name', '=', 'Tom Hanks')

The above code will create the following Cypher query:

MATCH (person:Person) WHERE person.name = "Tom Hanks" RETURN person

Available Clauses

Below are example of how to use the currently available clauses:


Create a match clause for your query:

public function match(string $label, string $variable = '') : void;
Cypher::query()->match('Person', 'person');


Create a where clause for your query:

public function where(string $variable, string $attribute, string $operator, $value, bool $or = false) : void

Create a simple one condition where statement:

Cypher::query()->where('person', 'attribute', '=', 'Some Value');

Create an AND where statement:

    ->where('person', 'attribute', '=', 'Some Value')
    ->where('person', 'email', '=', 'test@email.com');

Create an OR where statement:

    ->where('person', 'attribute', '=', 'Some Value')
    ->where('person', 'email', '=', 'test@email.com', true);

Currently I have only fully tested strings in where statements, but integers and doubles should also work with basic test coverage.


Create a return clause for your query:

public function return(string $variable, string $attribute = '') : void

Returning the entire node:


Returning a specific attribute:

Cypher::query()->return('person', 'age');

Other Clauses not yet completed

  • Create
  • Delete
  • Remove
  • Set


There is a composer script available to run the tests:

$ composer run test

However, if you are unable to run this please use the following command:

$ ./vendor/bin/phpunit --testdox


If you discover any security related issues, please email juststevemcd@gmail.com instead of using the issue tracker.