Reputation: 293
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?
Upvotes: 0
Views: 1331
Reputation: 20185
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');
}
Upvotes: 2