PHP code example of dwoodard / neo4j-eloquent

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

    

dwoodard / neo4j-eloquent example snippets




namespace App\Models\Neo4j;

use Neo4jEloquent\Model;

class User extends Model
{
    protected array $labels = ['User'];
    
    protected array $fillable = [
        'name',
        'email',
        'age',
        'city',
    ];
    
    protected array $casts = [
        'age' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];
}

// Create a user
$user = User::create([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'age' => 30,
    'city' => 'San Francisco'
]);

// Find all users
$users = User::all();

// Find a specific user
$user = User::find($userId);

// Query with conditions
$adults = User::label('User')
    ->where('age', '>', 18)
    ->orderBy('name')
    ->get();

use Neo4jEloquent\Node;

// Create any type of node
$product = Node::label('Product')->create([
    'name' => 'iPhone 15',
    'price' => 999,
    'category' => 'Electronics',
    'features' => ['5G', 'Face ID', 'Wireless Charging']
]);

// Multi-label nodes
$company = Node::label('Company', 'Organization')->create([
    'name' => 'Tech Innovations Inc',
    'industry' => 'Technology',
    'founded' => 2015
]);

// Query any nodes
$techProducts = Node::label('Product')
    ->where('category', 'Electronics')
    ->where('price', '>', 500)
    ->orderBy('price', 'desc')
    ->get();

// Find Alice's friends
$friends = Node::label('Person')
    ->where('name', 'Alice Johnson')
    ->outgoing('FRIENDS_WITH')
    ->label('Person')
    ->get();

// Find companies Alice works for
$employers = Node::label('Person')
    ->where('name', 'Alice Johnson')
    ->outgoing('WORKS_FOR')
    ->label('Company')
    ->get();

// Bidirectional relationships
$connections = Node::label('Person')
    ->where('name', 'Alice')
    ->related('KNOWS')
    ->label('Person')
    ->get();

$users = User::all();

// This will return properly formatted JSON with all node data
return response()->json($users);

// Or convert to array manually
$usersArray = $users->map(fn($user) => $user->toArray());

use Neo4jEloquent\Neo4jService;

$neo4j = app(Neo4jService::class);

$result = $neo4j->runRaw('
    MATCH (p:Person)-[:FRIENDS_WITH]->(f:Person)
    WHERE p.city = $city
    RETURN p.name, count(f) as friend_count
', ['city' => 'San Francisco']);



namespace App\Console\Commands;

use Illuminate\Console\Command;
use Neo4jEloquent\Node;

class TestNeo4j extends Command
{
    protected $signature = 'neo4j:test';
    protected $description = 'Test Neo4j connection';

    public function handle()
    {
        try {
            $testNode = Node::label('TestNode')->create([
                'name' => 'Test',
                'created_at' => now()
            ]);
            
            $this->info('✅ Neo4j connection successful!');
            
            // Clean up
            $testNode->delete();
            
        } catch (\Exception $e) {
            $this->error('❌ Neo4j connection failed: ' . $e->getMessage());
        }
    }
}

// Query nodes with multiple labels
$managers = Node::label('Person', 'Employee', 'Manager')
    ->where('department', 'Engineering')
    ->get();

// Add labels dynamically
$person = Node::label('Person')->find('uuid-123');
$person->addLabel('VIP');
$person->addLabel('Premium');

// Query with multiple conditions
$premiumCustomers = Node::label('Customer')
    ->where('total_spent', '>', 1000)
    ->where('account_status', 'premium')
    ->whereIn('country', ['US', 'CA', 'UK'])
    ->orderBy('total_spent', 'desc')
    ->limit(20)
    ->get();
bash
php artisan vendor:publish --provider="Neo4jEloquent\Laravel\Neo4jEloquentServiceProvider"
bash
php artisan make:command TestNeo4j