Download the PHP package digitalcloud/neo-eloquent without Composer
On this page you can find all versions of the php package digitalcloud/neo-eloquent. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package neo-eloquent
NeoEloquent
Neo4j Graph Eloquent Driver for Laravel
Chat & Support
Join the Official Neo4j Slack Group and use the #neo4j-php channel.
Quick Reference
- Installation
- Configuration
- Models
- Relationships
- Edges
- Migration
- Schema
- Aggregates
- Only in Neo
- Things To Avoid
Installation
Add the package to your composer.json
and run composer update
.
Laravel 5
5.6
5.5
5.4
5.3
5.2
5.1
5.0
Laravel 4
Add the service provider in app/config/app.php
:
The service provider will register all the required classes for this package and will also alias
the Model
class to NeoEloquent
so you can simply extend NeoEloquent
in your models.
Configuration
Connection
in app/config/database.php
or in case of an environment-based configuration app/config/[env]/database.php
make neo4j
your default connection:
Add the connection defaults:
Lumen
For Lumen you need to create a new folder called config
in the application root and there add a file called database.php
. There you will add the following code.
And add the following line in bootstrap/app.php
This is to enable Lumen to read other configurations other than the provided default ones.
In the case of adding the Service Provider. You must add it in the Register Providers section of bootstrap/app.php
. You can add it like so:
Migration Setup
If you're willing to have migrations:
- create the folder
app/database/labels
- modify
composer.json
and addapp/database/labels
to theclassmap
array - run
composer dump-autoload
Documentation
Models
- Node Labels
- Soft Deleting
As simple as it is, NeoEloquent will generate the default node label from the class name,
in this case it will be :User
. Read about node labels here
Namespaced Models
When you use namespaces with your models the label will consider the full namespace.
The generated label from that relationship will be VinelabCmsAdmin
, this is necessary to make sure
that labels do not clash in cases where we introduce another Admin
instance like
Vinelab\Blog\Admin
then things gets messy with :Admin
in the database.
Custom Node Labels
You may specify the label(s) you wish to be used instead of the default generated, they are also case sensitive so they will be stored as put here.
NeoEloquent has a fallback support for the $table
variable that will be used if found and there was no $label
defined on the model.
Do not worry about the labels formatting, You may specify them as array('Label1', 'Label2')
or separate them by a column :
and prepending them with a :
is optional.
Soft Deleting
Laravel 5
To enable soft deleting you'll need to use Vinelab\NeoEloquent\Eloquent\SoftDeletes
instead of Illuminate\Database\Eloquent\SoftDeletes
and just like Eloquent you'll need the $dates
in your models as follows:
Laravel 4
To enable soft deleting you'll need to use Vinelab\NeoEloquent\Eloquent\SoftDeletingTrait
instead of Illuminate\Database\Eloquent\SoftDeletingTrait
and just like Eloquent you'll need the $dates
in your models as follows:
Relationships
- One-To-One
- One-To-Many
- Many-To-Many
- Polymorphic
Let's go through some examples of relationships between Nodes.
One-To-One
This represents an OUTGOING
relationship direction from the :User
node to a :Phone
.
Saving
The Cypher performed by this statement will be as follows:
Defining The Inverse Of This Relation
This represents an INCOMING
relationship direction from
the :User
node to this :Phone
node.
Associating Models
Due to the fact that we do not deal with foreign keys, in our case it is much more than just setting the foreign key attribute on the parent model. In Neo4j (and Graph in general) a relationship is an entity itself that can also have attributes of its own, hence the introduction of Edges
Note: Associated models does not persist relations automatically when calling
associate()
.
The Cypher performed by this statement will be as follows:
One-To-Many
This represents an OUTGOING
relationship direction
from the :User
node to the :Post
node.
Similar to One-To-One
relationships the returned value from a save()
statement is an
Edge[In|Out]
The Cypher performed by this statement will be as follows:
Defining The Inverse Of This Relation
This represents an INCOMING
relationship direction from
the :User
node to this :Post
node.
Many-To-Many
This represents an INCOMING
relationship between a :User
node and another :User
.
$jd
follows $mc
:
Or using the attach()
method:
The Cypher performed by this statement will be as follows:
$mc
follows $jd
back:
The Cypher performed by this statement will be as follows:
get the followers of $jd
The Cypher performed by this statement will be as follows:
Dynamic Properties
Polymorphic
The concept behind Polymorphic relations is purely relational to the bone but when it comes to graph we are representing it as a HyperEdge.
Hyper edges involves three models, the parent model, hyper model and related model represented in the following figure:
Similarly in code this will be represented by three models User
Comment
and Post
where a User
with id 1 posts a Post
and a User
with id 6 COMMENTED
a Comment
ON
that Post
as follows:
In order to keep things simple but still involving the three models we will have to pass the
$morph
which is any commentable
model, in our case it's either a Video
or a Post
model.
Note: Make sure to have it defaulting to
null
so that we can Dynamicly or Eager load with$user->comments
later on.
Creating a Comment
with the create()
method.
As usual we will have returned an Edge, but this time it's not directed it is an instance of
HyperEdge
, read more about HyperEdges here.
Or you may save a Comment instance:
Also all the functionalities found in a BelongsToMany
relationship are supported like
attaching models by Ids:
Or detaching models:
Sync too:
Retrieving Polymorphic Relations
From our previous example we will use the Video
model to retrieve their comments:
Dynamicly Loading Morph Model
Eager Loading Morph Model
Retrieving The Inverse of a Polymorphic Relation
You may also specify the type of morph you would like returned:
Polymorphic Relations In Short
To drill things down here's how our three models involved in a Polymorphic relationship connect:
Eager Loading
Loading authors with their books with the least performance overhead possible.
Only two Cypher queries will be run in the loop above:
Edges
- EdgeIn
- EdgeOut
- HyperEdge
- Working with Edges
- Edge Attributes
Introduction
Due to the fact that relationships in Graph are much different than other database types so we will have to handle them accordingly. Relationships have directions that can vary between In and Out respectively towards the parent node.
EdgeIn
Represents an INCOMING
direction relationship from the related model towards the parent model.
To associate a User
to a Location
:
which in Cypher land will map to (:Location)<-[:LOCATED_AT]-(:User)
and $relation
being an instance of EdgeIn
representing an incoming relationship towards the parent.
And you can still access the models from the edge:
EdgeOut
Represents an OUTGOING
direction relationship from the parent model to the related model.
To save an outgoing edge from :User
to :Post
it goes like:
Which in Cypher would be (:User)-[:POSTED]->(:Post)
and $posted
being the EdgeOut
instance.
And fetch the related models:
HyperEdge
This edge comes as a result of a Polymorphic Relation representing an edge involving
two other edges left and right that can be accessed through the left()
and right()
methods.
This edge is treated a bit different than the others since it is not a direct relationship between two models which means it has no specific direction.
Working With Edges
As stated earlier Edges are entities to Graph unlike SQL where they are a matter of a
foreign key having the value of the parent model as an attribute on the belonging model or in
Documents where they are either embeds or ids as references. So we developed them to be light
models which means you can work with them as if you were working with an Eloquent
instance - to a certain extent,
except HyperEdges.
In the case of a HyperEdge
you can access all three models as follows:
Edge Attributes
By default, edges will have the timestamps created_at
and updated_at
automatically set and updated only if timestamps are enabled by setting $timestamps
to true
on the parent model.
Retrieve an Edge from a Relation
The same way an association will create an EdgeIn
relationship we can retrieve
the edge between two models by calling the edge($model)
method on the belongsTo
relationship.
You may also specify the model at the other side of the edge.
Note: By default NeoEloquent will try to perform the
$location->user
internally to figure out the related side of the edge based on the relation function name, in this case it'suser()
.
Only in Neo
- CreateWith
Here you will find NeoEloquent-specific methods and implementations that with the wonderful Eloquent methods would make working with Graph and Neo4j a blast!
CreateWith
- Creating Relations
- Attaching Relations
This method will "kind of" fill the gap between relational and document databases, it allows the creation of multiple related models with one database hit.
Creating New Records and Relations
Here's an example of creating a post with attached photos and videos:
The keys
photos
andvideos
must be the same as the relation method names in thePost
model.
The Cypher query performed by the example above is:
We will get the nodes created with their relations as such:
You may also mix models and attributes as relation values but it is not necessary
since NeoEloquent will pass the provided attributes through the $fillable
filter pipeline:
You may also use a single array of attributes as such:
Attaching Existing Records as Relations
createWith
is intelligent enough to know the difference when you pass an existing model,
a model Id or new records that you need to create which allows mixing new records with existing ones.
And we will get the Post
related to the existing Tag
nodes.
Or using the id
of the model:
The Cypher for the query that attaches records would be:
Migration
For migrations to work please perform the following:
- create the folder
app/database/labels
- modify
composer.json
and addapp/database/labels
to theclassmap
array
Since Neo4j is a schema-less database you don't need to predefine types of properties for labels. However you will be able to perform Indexing and Constraints using NeoEloquent's pain-less Schema.
Commands
NeoEloquent introduces new commands under the neo4j
namespace so you can still use Eloquent's migration commands side-by-side.
Migration commands are the same as those of Eloquent, in the form of neo4j:migrate[:command]
neo4j:make:migration Create a new migration file
neo4j:migrate Run the database migrations
neo4j:migrate:reset Rollback all database migrations
neo4j:migrate:refresh Reset and re-run all migrations
neo4j:migrate:rollback Rollback the last database migration
Creating Migrations
Like in Laravel you can create a new migration by using the make
command with Artisan:
php artisan neo4j:migrate:make create_user_label
Label migrations will be placed in app/database/labels
You can add additional options to commands like:
php artisan neo4j:migrate:make foo --path=app/labels
php artisan neo4j:migrate:make create_user_label --create=User
php artisan neo4j:migrate:make create_user_label --label=User
Running Migrations
Run All Outstanding Migrations
php artisan neo4j:migrate
Run All Outstanding Migrations For A Path
php artisan neo4j:migrate --path=app/foo/labels
Run All Outstanding Migrations For A Package
php artisan neo4j:migrate --package=vendor/package
Note: If you receive a "class not found" error when running migrations, try running the
composer dump-autoload
command.
Forcing Migrations In Production
To force-run migrations on a production database you can use:
php artisan neo4j:migrate --force
Rolling Back Migrations
Rollback The Last Migration Operation
php artisan neo4j:migrate:rollback
Rollback all migrations
php artisan neo4j:migrate:reset
Rollback all migrations and run them all again
php artisan neo4j:migrate:refresh
php artisan neo4j:migrate:refresh --seed
Schema
NeoEloquent will alias the Neo4jSchema
facade automatically for you to be used in manipulating labels.
If you decide to write Migration classes manually (not using the generator) make sure to have these use
statements in place:
use Vinelab\NeoEloquent\Schema\Blueprint;
use Vinelab\NeoEloquent\Migrations\Migration;
Currently Neo4j supports UNIQUE
constraint and INDEX
on properties. You can read more about them at
http://docs.neo4j.org/chunked/stable/graphdb-neo4j-schema.html
Schema Methods
Command | Description |
---|---|
$label->unique('email') |
Adding a unique constraint on a property |
$label->dropUnique('email') |
Dropping a unique constraint from property |
$label->index('uuid') |
Adding index on property |
$label->dropIndex('uuid') |
Dropping index from property |
Droping Labels
Renaming Labels
Checking Label's Existence
Checking Relation's Existence
You can read more about migrations and schema on:
http://laravel.com/docs/schema
http://laravel.com/docs/migrations
Aggregates
In addition to the Eloquent builder aggregates, NeoEloquent also has support for Neo4j specific aggregates like percentile and standard deviation, keeping the same function names for convenience. Check the docs for more.
table()
represents the label of the model
Changelog
Check the Releases for details.
Avoid
Here are some constraints and Graph-specific gotchas, a list of features that are either not supported or not recommended.
JOINS :confounded:
- They make no sense for Graph, plus Graph hates them!
Which makes them unsupported on purpose. If migrating from an
SQL
-based app they will be your boogie monster.
Pivot Tables in Many-To-Many Relationships
This is not supported, instead we will be using Edges to work with relationships between models.
Nested Arrays and Objects
- Due to the limitations imposed by the objects map types that can be stored in a single, you can never have nested arrays or objects in a single model, make sure it's flat. Example:
Check out the createWith() method on how you can achieve this in a Graph way.
Tests
- install a Neo4j instance and run it with the default configuration
localhost:7474
- make sure the database graph is empty to avoid conflicts
- after running
composer install
there should be/vendor/bin/phpunit
- run
./vendor/bin/phpunit
after making sure that the Neo4j instance is running
Tests marked as incomplete means they are either known issues or non-supported features, check included messages for more info.
All versions of neo-eloquent with dependencies
illuminate/support Version 5.7.*
illuminate/database Version 5.7.*
illuminate/pagination Version 5.7.*
illuminate/events Version 5.7.*
illuminate/console Version 5.7.*
heydavid713/neo4jphp Version 0.1.*
nesbot/carbon Version ~1.0