> For the complete documentation index, see [llms.txt](https://docs.ecotone.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ecotone.tech/modules/laravel/database-connection-dbal-module.md).

# Database Connection (DBAL Module)

Database connection with DBAL module in Laravel

We can use Ecotone's Laravel integration to reuse Connections that are already defined in your Application.

## **Using Existing Laravel Connection \[Recommended]**

In "**config/database.php"** we do have list of available connections in our Laravel Application, for example:

```php
'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
    ],
(...)
```

with this using ServiceContext we may tell Ecotone to use this Connection:

```php
class EcotoneConfiguration
{
    #[ServiceContext]
    public function laravelConnection(): LaravelConnectionReference
    {
        return LaravelConnectionReference::defaultConnection('mysql');
    }
}
```

{% hint style="success" %}
Reusing same connection as we use in Application, ensures database transactions will be rolled back correctly in case of any failure.\
\
It's all we need to configure. Ecotone will now know to use **"mysql"** as default.
{% endhint %}

## **Using DSN**

If we don't have existing connection defined, we can make use of DSN directly

```php
# Register Service in Provider

use Enqueue\Dbal\DbalConnectionFactory;
use Ecotone\Dbal\DbalConnection;

public function register()
{
     $this->app->singleton(DbalConnectionFactory::class, function () {
         return DbalConnection::fromDsn('pgsql://user:password@host:5432/db_name');
     });
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ecotone.tech/modules/laravel/database-connection-dbal-module.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
