### Fetch users for many-to-many example Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Fetches two User models to demonstrate the many-to-many relationship. ```php $jd = User::find(1012); $mc = User::find(1013); ``` -------------------------------- ### Avoid Nested Arrays/Objects in Model Creation (User::create) Source: https://github.com/vinelab/neoeloquent/blob/main/README.md This example shows an unsupported pattern: creating a model with nested arrays or objects. Due to limitations of object map types, models must be flat. Use the createWith() method to achieve nested structures in a Graph way. ```php // Don't User::create(['name' => 'Some Name', 'location' => ['lat' => 123, 'lng'=> -123 ] ]); ``` -------------------------------- ### Get followers with dynamic property Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Retrieves the followers of $jd using the dynamic property accessor. ```php $followers = $jd->followers; ``` -------------------------------- ### Seed the movie database with Cypher Source: https://github.com/vinelab/neoeloquent/blob/main/Examples/Movies/README.md This Cypher script creates the movie nodes (The Matrix trilogy), actor nodes, and ACTS_IN relationships with roles. Run this in your Neo4j database before starting the PHP example to populate the data. ```cypher CREATE (matrix1:Movie { title : 'The Matrix', year : '1999-03-31' }) CREATE (matrix2:Movie { title : 'The Matrix Reloaded', year : '2003-05-07' }) CREATE (matrix3:Movie { title : 'The Matrix Revolutions', year : '2003-10-27' }) CREATE (keanu:Actor { name:'Keanu Reeves' }) CREATE (laurence:Actor { name:'Laurence Fishburne' }) CREATE (carrieanne:Actor { name:'Carrie-Anne Moss' }) CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix1) CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix2) CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix3) CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix1) CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix2) CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix3) CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix1) CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix2) CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix3) ``` -------------------------------- ### Access related model dynamically Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Shows how to access related models dynamically. You can get the related model directly or access its attributes. ```php class Phone extends NeoEloquent { public function user() { return $this->belongsTo('User'); } } $phone = Phone::find(1006); $user = $phone->user; // or getting an attribute out of the related model $name = $phone->user->name; ``` -------------------------------- ### Retrieve inverse polymorphic relation Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Retrieves the parent model of a comment using the commentable relationship. Also shows eager loading with Comment::with('commentable')->get(). ```php $postComment = Comment::find(7); $post = $comment->commentable; $videoComment = Comment::find(5); $video = $comment->commentable; // You can also eager load them Comment::with('commentable')->get(); ``` -------------------------------- ### Use options with neo4j:make:migration Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Shows additional options for the make:migration command: --path to specify a custom directory, --create to create a label, and --label to set the label name. ```bash php artisan neo4j:make:migration foo --path=app/labels php artisan neo4j:make:migration create_user_label --create=User php artisan neo4j:make:migration create_user_label --label=User ``` -------------------------------- ### Create post with photos and videos using Post::createWith Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a Post model with photos and videos relations, then uses Post::createWith to create the post and its related records in one database hit. The keys 'photos' and 'videos' must match the relation method names. The Cypher query shows the resulting CREATE statement. ```php class Post extends NeoEloquent { public function photos() { return $this->hasMany('Photo', 'PHOTO'); } public function videos() { return $this->hasMany('Video', 'VIDEO'); } } ``` ```php Post::createWith(['title' => 'the title', 'body' => 'the body'], [ 'photos' => [ [ 'url' => 'http://url', 'caption' => '...', 'metadata' => '...' ], [ 'url' => 'http://other.url', 'caption' => 'the bay', 'metadata' => '...' ] ], 'videos' => [ 'title' => 'Boats passing us by', 'description' => '...' ] ]); ``` ```cypher CREATE (post:`Post` {title: 'the title', body: 'the body'}), (post)-[:PHOTO]->(:`Photo` {url: 'http://url', caption: '...', metadata: '...'}), (post)-[:PHOTO]->(:`Photo` {url: 'http://other', caption: 'the bay', metadata: '...'}), (post)-[:VIDEO]->(:`Video` {title: 'Boats passing us by', description: '...'}); ``` -------------------------------- ### Create and save an edge with attributes Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Shows how to create an edge via associate() and set an attribute before saving. The save() returns true on success. ```php $edge = $location->associate($user); $edge->last_visited = 'today'; $edge->save(); // true ``` -------------------------------- ### Force migrations in production Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Forces migrations to run on a production database. ```bash php artisan neo4j:migrate --force ``` -------------------------------- ### Run migrations for a package Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Runs all outstanding migrations for a package. ```bash php artisan neo4j:migrate --package=vendor/package ``` -------------------------------- ### List NeoEloquent migration commands Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Lists the NeoEloquent migration commands available under the neo4j namespace, mirroring Eloquent's migration commands. ```bash 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 ``` -------------------------------- ### Run all outstanding migrations Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Runs all outstanding migrations. ```bash php artisan neo4j:migrate ``` -------------------------------- ### Create a label migration with Artisan Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Creates a new migration file for a label. The generated file is placed in app/database/labels. ```bash php artisan neo4j:make:migration create_user_label ``` -------------------------------- ### Configure Neo4j Connection Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Add the neo4j connection defaults in the database configuration. Remember to update your ENV variables (.env) if you use them. ```php 'connections' => [ 'neo4j' => [ 'driver' => 'neo4j', 'host' => 'localhost', 'port' => 7687, 'username' => 'username', 'password' => 'password', ] ] ``` -------------------------------- ### Create and save an edge as a light model Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Shows that edges are light models: create with associate() and save with save(). The comment shows the EdgeIn class name. ```php // Create a new relationship $relation = $location->associate($user); // Vinelab\NeoEloquent\Eloquent\Edges\EdgeIn // Save the relationship to the database $relation->save(); // true ``` -------------------------------- ### Run migrations for a custom path Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Runs all outstanding migrations for a specific path. ```bash php artisan neo4j:migrate --path=app/foo/labels ``` -------------------------------- ### Create user with single account relation using User::createWith Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Creates a User with a single related Account using a single array of attributes for the relation. ```php class User extends NeoEloquent { public function account() { return $this->hasOne('Account'); } } User::createWith(['name' => 'foo'], ['account' => ['guid' => 'bar', 'email' => 'some@mail.net']]); ``` -------------------------------- ### Set Default Connection to neo4j Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Set neo4j as the default connection in app/config/database.php or environment-based configuration. ```php 'default' => 'neo4j', ``` -------------------------------- ### Attach follower with attach() Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Attaches a follower using attach(). You can pass either the model instance or its primary key. ```php $jd->followers()->attach($mc); // Or.. $jd->followers()->attach(1013); // 1013 being the id of $mc ($mc->getKey()) ``` -------------------------------- ### Register NeoEloquent Service Provider Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Add the service provider in app/config/app.php. It registers all required classes and aliases the Model class to NeoEloquent. ```php Vinelab\NeoEloquent\NeoEloquentServiceProvider::class, ``` -------------------------------- ### Associate a User to a Location (EdgeIn) Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Associates a User to a Location, creating an EdgeIn instance. The resulting Cypher is (:Location)<-[:LOCATED_AT]-(:User). ```php $location = Location::find(1922); $user = User::find(3876); $relation = $location->associate($user); ``` -------------------------------- ### Save follower with save() Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Saves $mc as a follower of $jd using the save() method. ```php $jd->followers()->save($mc); ``` -------------------------------- ### Add NeoEloquent to composer.json Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Add the package to your composer.json and run composer update. ```json { "require": { "vinelab/neoeloquent": "1.8.*" } } ``` -------------------------------- ### Create and access a HyperEdge Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Creates a HyperEdge via attach() and accesses the left and right edges using left() and right(). HyperEdge has no specific direction. ```php $edge = $user->comments($post)->attach($comment); // Access the left and right edges $left = $edge->left(); $user = $left->parent(); $comment = $left->related(); $right = $edge->right(); $comment = $right->parent(); $post = $right->related(); ``` -------------------------------- ### Polymorphic relation summary: Post model Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Shows the Post model (and Video is the same) in the polymorphic relationship summary. ```php class Post extends NeoEloquent { // Video is the same as this one public function comments() { return $this->morphMany('Comment', 'ON'); } } ``` -------------------------------- ### Refresh migrations with optional seed Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Rolls back all migrations and runs them again. The --seed option also runs the database seeder. ```bash php artisan neo4j:migrate:refresh php artisan neo4j:migrate:refresh --seed ``` -------------------------------- ### Dynamically load morphMany relationship Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Dynamically loads the comments relationship for a Video instance. ```php $video = Video::find(3); $comments = $video->comments; ``` -------------------------------- ### Soft deleting with SoftDeletingTrait Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Enables soft deleting by using the NeoEloquent SoftDeletingTrait and including the 'deleted_at' date in the $dates property. ```php use Vinelab\NeoEloquent\Eloquent\SoftDeletingTrait; class User extends NeoEloquent { use SoftDeletingTrait; protected $dates = ['deleted_at']; } ``` -------------------------------- ### Attach existing Tag models to a new Post using Post::createWith Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a Post model with a tags relation, then creates two Tag models and attaches them to a new Post using Post::createWith. The method distinguishes existing models from new records. ```php class Post extends NeoEloquent { public function tags() { return $this->hasMany('Tag', 'TAG'); } } ``` ```php $tag1 = Tag::create(['title' => 'php']); $tag2 = Tag::create(['title' => 'dev']); $post = Post::createWith(['title' => 'foo', 'body' => 'bar'], ['tags' => [$tag1, $tag2]]); ``` -------------------------------- ### Custom node label and model creation Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a model with a custom node label and fillable attributes, then creates a record. Labels are case sensitive and can be a string or an array. ```php use NeoEloquent; class User extends NeoEloquent { protected $label = 'User'; // or array('User', 'Fan') protected $fillable = ['name', 'email']; } $user = User::create(['name' => 'Some Name', 'email' => 'some@email.com']); ``` -------------------------------- ### Check label existence Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Checks if the 'User' label exists. ```php if (Neo4jSchema::hasLabel('User')) { } else { } ``` -------------------------------- ### Attach existing records by ID using Post::createWith Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Attaches existing records by their model IDs using Post::createWith. The Cypher query matches Tag nodes with IDs 1 and 2 and creates the TAG relationship. ```php Post::createWith(['title' => 'foo', 'body' => 'bar'], ['tags' => 1, 'privacy' => 2]); ``` ```cypher CREATE (post:`Post` {title: 'foo', 'body' => 'bar'}) WITH post MATCH (tag:`Tag`) WHERE id(tag) IN [1, 2] CREATE (post)-[:TAG]->(tag); ``` -------------------------------- ### Rollback all migrations Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Rolls back all migrations. ```bash php artisan neo4j:migrate:reset ``` -------------------------------- ### Define one-to-one relationship with hasOne Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a one-to-one relationship from a User node to a Phone node. The relationship direction is OUTGOING from the :User node to the :Phone node. ```php class User extends NeoEloquent { public function phone() { return $this->hasOne('Phone'); } ``` -------------------------------- ### Save reverse follower with save() Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Saves $jd as a follower of $mc, making the relationship bidirectional. ```php $mc->followers()->save($jd); ``` -------------------------------- ### Basic NeoEloquent model Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a basic NeoEloquent model. The default node label is generated from the class name, resulting in ':User'. ```php use NeoEloquent; class User extends NeoEloquent {} ``` -------------------------------- ### Mix model instances and attributes in Post::createWith Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Shows mixing model instances and attributes as relation values. NeoEloquent passes provided attributes through the $fillable filter pipeline, so it is not necessary to pre-filter. ```php $videos = new Video(['title' => 'foo', 'description' => 'bar']); Post::createWith($info, compact('videos')); ``` -------------------------------- ### Define morphMany relationship on Video model Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines the inverse morphMany relationship on the Video model to retrieve comments. ```php class Video extends NeoEloquent { public function comments() { return $this->morphMany('Comment', 'ON'); } } ``` -------------------------------- ### Cypher for associate() Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Cypher query generated when associating an Account with a User. It uses MERGE to create the ACCOUNT relationship. ```cypher MATCH (account:`Account`), (user:`User`) WHERE id(account) = 1986 AND id(user) = 9862 MERGE (account)<-[rel_user_account:ACCOUNT]-(user) RETURN rel_user_account; ``` -------------------------------- ### Access all models from a HyperEdge Source: https://github.com/vinelab/neoeloquent/blob/main/README.md For a HyperEdge, access all three models: parent(), hyper(), and related(). ```php $edge = $user->comments($post)->save($comment); $user = $edge->parent(); $comment = $edge->hyper(); $post = $edge->related(); ``` -------------------------------- ### Define typed morphTo relationships Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines specific morphTo relationships for Post and Video, allowing you to specify the type of morph returned. ```php class Comment extends NeoEloquent { public function post() { return $this->morphTo('Post', 'ON'); } public function video() { return $this->morphTo('Video', 'ON'); } } ``` -------------------------------- ### NeoEloquent Aggregates: count, countDistinct, max, min, avg, sum, percentileDisc, percentileCont, stdev, stdevp, collect Source: https://github.com/vinelab/neoeloquent/blob/main/README.md NeoEloquent provides Neo4j-specific aggregates like percentileDisc, percentileCont, stdev, stdevp, and collect, in addition to standard Eloquent aggregates. The table() method represents the label of the model. Use these methods to perform aggregations on Neo4j nodes. ```php $users = DB::table('User')->count(); $distinct = DB::table('User')->countDistinct('points'); $price = DB::table('Order')->max('price'); $price = DB::table('Order')->min('price'); $price = DB::table('Order')->avg('price'); $total = DB::table('User')->sum('votes'); $disc = DB::table('User')->percentileDisc('votes', 0.2); $cont = DB::table('User')->percentileCont('votes', 0.8); $deviation = DB::table('User')->stdev('sex'); $population = DB::table('User')->stdevp('sex'); $emails = DB::table('User')->collect('email'); ``` -------------------------------- ### Associate models with associate() and save Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Associates an existing model using associate(). Note that associate() does not persist the relation automatically; you must call save() on the returned EdgeIn object. ```php $account = Account::find(1986); // $relation will be Vinelab\NeoEloquent\Eloquent\Edges\EdgeIn $relation = $user->account()->associate($account); // Save the relation $relation->save(); ``` -------------------------------- ### Cypher queries for eager loading Source: https://github.com/vinelab/neoeloquent/blob/main/README.md The two Cypher queries executed when eager loading the author relationship. ```cypher MATCH (book:`Book`) RETURN *; MATCH (book:`Book`), (book)<-[:WROTE]-(author:`Author`) WHERE id(book) IN [1, 2, 3, 4, 5, ...] RETURN book, author; ``` -------------------------------- ### Define label with unique constraint Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a label 'User' with a unique constraint on the 'uuid' property using the Neo4jSchema facade. ```php Neo4jSchema::label('User', function(Blueprint $label) { $label->unique('uuid'); }); ``` -------------------------------- ### Set edge attributes and access timestamps Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Sets custom attributes on an edge and saves it. Timestamps created_at and updated_at are automatically set only if $timestamps is true on the parent model; they are Carbon\Carbon instances. ```php $located_at = $location->associate($user); $located_at->since = 1966; $located_at->present = true; $located_at->save(); // $created_at and $updated_at are Carbon\Carbon instances $created_at = $located_at->created_at; $updated_at = $located_at->updated_at; ``` -------------------------------- ### Cypher for reverse save() Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Cypher query generated when $mc follows $jd back. It creates a FOLLOWS relationship in the opposite direction. ```cypher MATCH (user:`User`), (followers:`User`) WHERE id(user) = 1013 AND id(followers) = 1012 CREATE (user)-[rel_user_followers:FOLLOWS]->(followers) RETURN rel_follows; ``` -------------------------------- ### Define one-to-many relationship with hasMany Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a one-to-many relationship from a User to Post nodes using the POSTED relationship type. The direction is OUTGOING. ```php class User extends NeoEloquent { public function posts() { return $this->hasMany('Post', 'POSTED'); } } ``` -------------------------------- ### Save a related model in one-to-one relationship Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Saves a new Phone model through the one-to-one relationship. The save() method returns an EdgeIn or EdgeOut object. ```php $phone = new Phone(['code' => 961, 'number' => '98765432']) $relation = $user->phone()->save($phone); ``` -------------------------------- ### Rename label Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Renames a label from $from to $to. ```php Neo4jSchema::renameLabel($from, $to); ``` -------------------------------- ### Namespaced NeoEloquent model Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a namespaced model. The generated label uses the full namespace, e.g., 'VinelabCmsAdmin', to avoid label clashes. ```php namespace App\Models; use NeoEloquent; class Admin extends NeoEloquent { } ``` -------------------------------- ### Create a Comment via polymorphic relation Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Creates a Comment through the polymorphic relationship. Returns a HyperEdge instance (not directed). ```php $user = User::find(6); $post = Post::find(2); $user->comments($post)->create(['text' => 'Totally agree!', 'likes' => 0, 'abuse' => 0]); ``` -------------------------------- ### Define polymorphic relationship with hyperMorph Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a polymorphic relationship on the User model using hyperMorph. The $morph parameter must default to null to allow dynamic or eager loading with $user->comments. ```php class User extends NeoEloquent { public function comments($morph = null) { return $this->hyperMorph($morph, 'Comment', 'COMMENTED', 'ON'); } } ``` -------------------------------- ### Attach models by IDs in polymorphic relation Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Attaches models by IDs using the attach method, similar to BelongsToMany relationships. ```php $user->comments($post)->attach([$id, $otherId]); ``` -------------------------------- ### Access models from an EdgeIn Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Accesses the parent and related models from an EdgeIn instance using parent() and related(). ```php $relation = $location->associate($user); $location = $relation->parent(); $user = $relation->related(); ``` -------------------------------- ### Eager load morphMany relationship Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Eager loads the comments relationship to avoid N+1 queries. ```php $video = Video::with('comments')->find(3); foreach ($video->comments as $comment) { // } ``` -------------------------------- ### Fallback $table for node label Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a model using the $table variable as a fallback for the node label when no $label is defined. ```php use NeoEloquent; class User extends NeoEloquent { protected $table = 'User'; } ``` -------------------------------- ### Cypher for save() in many-to-many Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Cypher query generated when $jd follows $mc. It creates a FOLLOWS relationship from the follower to the user. ```cypher MATCH (user:`User`), (followers:`User`) WHERE id(user) = 1012 AND id(followers) = 1013 CREATE (followers)-[:FOLLOWS]->(user) RETURN rel_follows; ``` -------------------------------- ### Access models from an EdgeOut Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Fetches the related models from an EdgeOut instance using parent() and related(). ```php $edge = $user->posts()->save($post); $user = $edge->parent(); $post = $edge->related(); ``` -------------------------------- ### Drop labels Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Drops the 'User' label unconditionally or only if it exists. ```php Neo4jSchema::drop('User'); Neo4jSchema::dropIfExists('User'); ``` -------------------------------- ### Save an outgoing edge (EdgeOut) Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Saves an outgoing edge from :User to :Post. The resulting Cypher is (:User)-[:POSTED]->(:Post) and $posted is an EdgeOut instance. ```php $post = new Post(['...']); $posted = $user->posts()->save($post); ``` -------------------------------- ### Define many-to-many with belongsToMany Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a many-to-many relationship between User nodes using the FOLLOWS relationship type. The direction is INCOMING. ```php class User extends NeoEloquent { public function followers() { return $this->belongsToMany('User', 'FOLLOWS'); } } ``` -------------------------------- ### Define inverse one-to-one with belongsTo Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines the inverse of the one-to-one relationship. The direction is INCOMING from the :User node to the :Phone node. ```php class Phone extends NeoEloquent { public function user() { return $this->belongsTo('User'); } } ``` -------------------------------- ### Check relation existence Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Checks if the 'FRIEND_OF' relation exists. ```php if (Neo4jSchema::hasRelation('FRIEND_OF')) { } else { } ``` -------------------------------- ### Cypher for one-to-one save Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Cypher query generated when saving a Phone through a one-to-one relationship. It creates a PHONE relationship from the User node to the Phone node. ```cypher MATCH (user:`User`) WHERE id(user) = 1 CREATE (user)-[:PHONE]->(phone:`Phone` {code: 961, number: '98765432', created_at: 7543788, updated_at: 7543788}) RETURN phone; ``` -------------------------------- ### Rollback the last migration Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Rolls back the last migration operation. ```bash php artisan neo4j:migrate:rollback ``` -------------------------------- ### Sync polymorphic relation with IDs Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Syncs the relationship to the given IDs, removing any that are not in the list. ```php $user->comments($post)->sync([$id, $otherId, $someId]); ``` -------------------------------- ### Save a Comment instance via polymorphic relation Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Saves an existing Comment instance through the polymorphic relationship. ```php $comment = new Comment(['text' => 'Magnificent', 'likes' => 0, 'abuse' => 0]); $user->comments($post)->save($comment); ``` -------------------------------- ### Define belongsTo relationship on Book model Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a belongsTo relationship on the Book model to its Author. ```php class Book extends NeoEloquent { public function author() { return $this->belongsTo('Author'); } } ``` -------------------------------- ### Eager load belongsTo relationship Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Eager loads the author relationship to minimize performance overhead. Only two Cypher queries are executed. ```php foreach (Book::with('author')->get() as $book) { echo $book->author->name; } ``` -------------------------------- ### Cypher for one-to-many save Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Cypher query generated when saving a Post through a one-to-many relationship. It creates a POSTED relationship. ```cypher MATCH (user:`User`) WHERE id(user) = 1 CREATE (user)-[rel_user_post:POSTED]->(post:`Post` {title: 'The Title', body: 'Hot Body', created_at: '15-05-2014', updated_at: '15-05-2014'}) RETURN rel_user_post; ``` -------------------------------- ### Define a hasMany relationship for EdgeOut Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a hasMany relationship that produces an EdgeOut (outgoing direction) from the parent model to the related model. The relationship name is 'POSTED'. ```php class User extends NeoEloquent { public function posts() { return $this->hasMany('Post', 'POSTED'); } } ``` -------------------------------- ### Define inverse one-to-many with belongsTo Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines the inverse of the one-to-many relationship. The direction is INCOMING from the :User node to the :Post node. ```php class Post extends NeoEloquent { public function author() { return $this->belongsTo('User', 'POSTED'); } } ``` -------------------------------- ### Define morphTo relationship on Comment model Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines the inverse polymorphic relationship on the Comment model using morphTo. ```php class Comment extends NeoEloquent { public function commentable() { return $this->morphTo(); } } ``` -------------------------------- ### Cypher for retrieving followers Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Cypher query generated when retrieving followers. It matches the FOLLOWS relationship in either direction. ```cypher MATCH (user:`User`), (followers:`User`), (user)-[rel_user_followers:FOLLOWS]-(followers) WHERE id(user) = 1012 RETURN rel_follows; ``` -------------------------------- ### Define a belongsTo relationship for EdgeIn Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Defines a belongsTo relationship that produces an EdgeIn (incoming direction) from the related model to the parent. The relationship name is 'LOCATED_AT'. ```php class Location extends NeoEloquent { public function user() { return $this->belongsTo('User', 'LOCATED_AT'); } } ``` -------------------------------- ### Save a related model in one-to-many Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Saves a new Post through the one-to-many relationship. The save() returns an EdgeIn or EdgeOut object. ```php $user = User::find(1); $post = new Post(['title' => 'The Title', 'body' => 'Hot Body']); $user->posts()->save($post); ``` -------------------------------- ### Detach a model from polymorphic relation Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Detaches a model from the polymorphic relationship. Accepts either the model instance or its ID. ```php $user->comments($post)->detach($comment); // or $comment->id ``` -------------------------------- ### Retrieve an edge with explicit related model Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Retrieves the edge by explicitly specifying the model at the other side. Without this, NeoEloquent internally calls $location->user to figure out the related side. ```php $location = Location::find(1892); $edge = $location->user()->edge($location->user); ``` -------------------------------- ### Retrieve an edge from a belongsTo relation Source: https://github.com/vinelab/neoeloquent/blob/main/README.md Retrieves the edge between two models by calling edge() on the belongsTo relationship. By default it uses the relation function name to determine the related side. ```php $location = Location::find(1892); $edge = $location->user()->edge(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.