doudang4857 2017-12-07 07:16
浏览 162

在laravel中连接图形DB和SQL

I want to connect multiple databases in Laravel 5.5. I tried using this but it doesn't seem to work. I want to use https://github.com/graphaware/neo4j-php-client as a client to connect to graph DB, the reason I wanted to use this was to run cypher queries directly.

What changes should I make in the config/database.php to accommodate such condition and how do I access each database from the code?

  • 写回答

1条回答 默认 最新

  • dskld5423 2017-12-07 09:08
    关注

    You can create a custom provider, for example in App\Providers\Neo4jServiceProvider.php and define 2 or more connections to the client :

    class Neo4jServiceProvider extends ServiceProvider
    {
        /**
         * Register the application services.
         *
         * @return void
         */
        public function register()
        {
            $this->app->singleton(Client::class, function() {
                return ClientBuilder::create()
                    ->addConnection('server1', getenv('NEO4J_1_HOST'))
                    ->addConnection('server2', getenv('NEO4J_2_HOST'))
                    ->build();
            });
        }
    }
    

    Then you register the provider in your application, add this to the providers array of the config/app.php file :

    App\Providers\Neo4jServiceProvider::class,
    

    Add the connections parameters for your neo4j dbs in the .env file.

    Next, you can inject this service anywhere and query any db :

    class MyController
    {
    
      private $client;
    
      public function __construct(Client $client)
      {
        $this->client = $client;
      }
    
      public function doSomeQueries()
      {
        // Query DB 1
        $result = $this->client->run('MATCH (n) RETURN count(n)', null, null, 'server1'); 
        // Query DB 2
        $result2 = $this->client->run('MATCH (n) RETURN count(n)', null, null, 'server2'); 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改