Retrieve edge and relationship properties using lumen

I'm new of lumen and neo4j, I'd like to add some properties to a specific relationship between two labels.

For example if we suppose that there is a many to many relationship between the Exhibit label and Zone label that is characterized by coordinates property, how can I retrieve the coordinates attribute?

Reading the neoEloquent documentation, I have understand that for retrieve the edge between two labels I have to follow a procedure like that there is present inside the function findZone:

<?php

    namespace App;

    use Vinelab\NeoEloquent\Eloquent\Model;

    class Exhibit extends Model{
        protected $label = 'Exhibit';

        protected $fillable = [];

        protected $hidden = [];

        public function zones(){
            return $this->belongsToMany('App\Zone', 'BELONGS_TO');
        }

        public static function findZone($exhibit_id){
            $exhibit = Exhibit::find($exhibit_id);
            $exhibit->zones();


            return $exhibit->zones()->edge();
        }
    }

The issue is that when I test this function the result is a empty response, but I'm sure that there is a edge between thats labels.

Am I wrong or the findZone function can be translate in Cypher as this query?

MATCH (e:Exhibit)-[edge:belongs_to]->(z:Zone) WHERE e.exhibit_id = {exhibit_id} RETURN edge 

Edit

As the user Sven Hakvoort suggest me, I have change

$this->belongsToMany('App\Zone', 'BELONGS_TO'); to $this->belongsToMany('App\Zone', 'belongs_to');

Now the response is a 500 Internal Error, stack trace:

[2018-10-12 14:33:41] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Argument 2 passed to Vinelab\NeoEloquent\Eloquent\Edges\Finder::first() must be an instance of Vinelab\NeoEloquent\Eloquent\Model, null given, called in E:\laravel-projects\api_certose\vendor\vinelab\neoeloquent\src\Eloquent\Relations\BelongsToMany.php on line 158 in E:\laravel-projects\api_certose\vendor\vinelab\neoeloquent\src\Eloquent\Edges\Finder.php:70
Stack trace:
#0 E:\laravel-projects\api_certose\vendor\vinelab\neoeloquent\src\Eloquent\Relations\BelongsToMany.php(158): Vinelab\NeoEloquent\Eloquent\Edges\Finder->first(Object(App\Exhibit), NULL, 'belongs_to')
#1 E:\laravel-projects\api_certose\app\Exhibit.php(23): Vinelab\NeoEloquent\Eloquent\Relations\BelongsToMany->edge()
#2 E:\laravel-projects\api_certose\app\Http\Controllers\ExhibitController.php(44): App\Exhibit::findZone('159')
#3 [internal function]: App\Http\Controllers\ExhibitController->retrieveZone('159')
#4 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(29): call_user_func_array(Array, Array)
#5 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#6 E:\laravel-projects\api_certose\vendor\illuminate\container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Laravel\Lumen\Application), Array, Object(Closure))
#7 E:\laravel-projects\api_certose\vendor\illuminate\container\Container.php(564): Illuminate\Container\BoundMethod::call(Object(Laravel\Lumen\Application), Array, Array, NULL)
#8 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(373): Illuminate\Container\Container->call(Array, Array)
#9 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(339): Laravel\Lumen\Application->callControllerCallable(Array, Array)
#10 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(313): Laravel\Lumen\Application->callLumenController(Object(App\Http\Controllers\ExhibitController), 'retrieveZone', Array)
#11 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(275): Laravel\Lumen\Application->callControllerAction(Array)
#12 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(260): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#13 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(230): Laravel\Lumen\Application->handleFoundRoute(Array)
#14 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(164): Laravel\Lumen\Application->handleDispatcherResponse(Array)
#15 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(413): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#16 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(166): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#17 E:\laravel-projects\api_certose\vendor\laravel\lumen-framework\src\Concerns\RoutesRequests.php(107): Laravel\Lumen\Application->dispatch(NULL)
#18 E:\laravel-projects\api_certose\public\index.php(28): Laravel\Lumen\Application->run()
#19 {main} {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Argument 2 passed to Vinelab\\NeoEloquent\\Eloquent\\Edges\\Finder::first() must be an instance of Vinelab\\NeoEloquent\\Eloquent\\Model, null given, called in E:\\laravel-projects\\api_certose\\vendor\\vinelab\
eoeloquent\\src\\Eloquent\\Relations\\BelongsToMany.php on line 158 at E:\\laravel-projects\\api_certose\\vendor\\vinelab\
eoeloquent\\src\\Eloquent\\Edges\\Finder.php:70)
[stacktrace]
#0 E:\\laravel-projects\\api_certose\\vendor\\vinelab\
eoeloquent\\src\\Eloquent\\Relations\\BelongsToMany.php(158): Vinelab\\NeoEloquent\\Eloquent\\Edges\\Finder->first(Object(App\\Exhibit), NULL, 'belongs_to')
#1 E:\\laravel-projects\\api_certose\\app\\Exhibit.php(23): Vinelab\\NeoEloquent\\Eloquent\\Relations\\BelongsToMany->edge()
#2 E:\\laravel-projects\\api_certose\\app\\Http\\Controllers\\ExhibitController.php(44): App\\Exhibit::findZone('159')
#3 [internal function]: App\\Http\\Controllers\\ExhibitController->retrieveZone('159')
#4 E:\\laravel-projects\\api_certose\\vendor\\illuminate\\container\\BoundMethod.php(29): call_user_func_array(Array, Array)
#5 E:\\laravel-projects\\api_certose\\vendor\\illuminate\\container\\BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#6 E:\\laravel-projects\\api_certose\\vendor\\illuminate\\container\\BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Laravel\\Lumen\\Application), Array, Object(Closure))
#7 E:\\laravel-projects\\api_certose\\vendor\\illuminate\\container\\Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Laravel\\Lumen\\Application), Array, Array, NULL)
#8 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(373): Illuminate\\Container\\Container->call(Array, Array)
#9 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(339): Laravel\\Lumen\\Application->callControllerCallable(Array, Array)
#10 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(313): Laravel\\Lumen\\Application->callLumenController(Object(App\\Http\\Controllers\\ExhibitController), 'retrieveZone', Array)
#11 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(275): Laravel\\Lumen\\Application->callControllerAction(Array)
#12 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(260): Laravel\\Lumen\\Application->callActionOnArrayBasedRoute(Array)
#13 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(230): Laravel\\Lumen\\Application->handleFoundRoute(Array)
#14 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(164): Laravel\\Lumen\\Application->handleDispatcherResponse(Array)
#15 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(413): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}()
#16 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(166): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))
#17 E:\\laravel-projects\\api_certose\\vendor\\laravel\\lumen-framework\\src\\Concerns\\RoutesRequests.php(107): Laravel\\Lumen\\Application->dispatch(NULL)
#18 E:\\laravel-projects\\api_certose\\public\\index.php(28): Laravel\\Lumen\\Application->run()
#19 {main}
"} 

1 answer

  • answered 2018-10-12 14:26 Sven Hakvoort

    Your PHP code is correct, however Neo4j is case-sensitive, try changing $this->belongsToMany('App\Zone', 'BELONGS_TO'); to $this->belongsToMany('App\Zone', 'belongs_to');

    And yes, your code translates to the query in your question.

    EDIT

    For the error you are getting try $exhibit->zones()->edge($exhibit->zones) instead of $exhibit->zones()->edge()