> 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/messaging/multi-tenancy-support/getting-started/laravel.md).

# Laravel

## Configuring Connections

While using Laravel we can reuse existing Connections (**config/database.php**).

```
return [
    'default' => 'tenant_a_connection',
    'connections' => [
        'tenant_a_connection' => [
            'url' => getenv('TENANT_A_DATABASE_URL')
        ],

        'tenant_b_connection' => [
            'url' => getenv('TENANT_B_DATABASE_URL')
        ],
    ]
];
```

And then we can set up Mapping:

```php
final readonly class EcotoneConfiguration
{
    #[ServiceContext]
    public function multiTenantConfiguration(): MultiTenantConfiguration
    {
        return MultiTenantConfiguration::create(
            tenantHeaderName: 'tenant', // Message header, where to look for tenant
            tenantToConnectionMapping: [
                'tenant_a' => LaravelConnectionReference::create('tenant_a_connection'),
                'tenant_b' => LaravelConnectionReference::create('tenant_b_connection')
            ],
        );
    }
}
```

## Sending Message in Context of Tenant

\`We've defined **tenantHeaderName** as **tenant** in our Mapping configuration. This means we can now pass tenant context under this name using Message Headers (metadata).

```php
final readonly class CustomerController extends Controller
{
    public function __construct(private CommandBus $commandBus) {}
    
    public function placeOrder(Request $request): Response
    {
        $this->commandBus->send(
            new RegisterCustomer($request->input('name')),
            metadata: [
                // This is the Message Header that will be used for mapping tenant
                'tenant' => 'tenant_a'
            ]  
        );
        
        return new Response(200);        
    }
}
```

## Defining Message Handlers

We define Message Handler the same way we would do it for Single Tenant application. Yet we need to be aware that we need to make use of correct Entity Manager for the job.

```php
final readonly class CustomerService
{
    #[CommandHandler]
    public function handle(RegisterCustomer $command)
    {
        Customer::register($command)->save();
    }
}
```

Like we can see here, there is no code being aware of Multi-Tenancy here. This is because Ecotone will understand the current context and will switch default database to Tenant's database.\
This way Customer will be stored in correct DB.


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ecotone.tech/messaging/multi-tenancy-support/getting-started/laravel.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
