PHPackages                             jgamboa/nile-laravel-server - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jgamboa/nile-laravel-server

ActiveLibrary[Database &amp; ORM](/categories/database)

jgamboa/nile-laravel-server
===========================

Package to connect to nile.dev PG Multi tenant

10[3 PRs](https://github.com/JGamboa/nile-laravel-server/pulls)PHPCI passing

Since Oct 13Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/JGamboa/nile-laravel-server)[ Packagist](https://packagist.org/packages/jgamboa/nile-laravel-server)[ RSS](/packages/jgamboa-nile-laravel-server/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

Package to connect to nile.dev PG Multi tenant
==============================================

[](#package-to-connect-to-niledev-pg-multi-tenant)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1a3f10608d7add8d0fe75f4a0a17adb57d82bc57169717ad19f4464a6c2983ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a67616d626f612f6e696c652d6c61726176656c2d7365727665722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jgamboa/nile-laravel-server)[![GitHub Tests Action Status](https://camo.githubusercontent.com/bb9039bdc9d3e271a7a21f13a46f5a9d90e87a60f3d338e1a3e68c8209e919d8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a67616d626f612f6e696c652d6c61726176656c2d7365727665722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/jgamboa/nile-laravel-server/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/70995f4980df360502e0fc06af29282aa1d46e0a1e45d054fc606907c82ba74d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a67616d626f612f6e696c652d6c61726176656c2d7365727665722f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/jgamboa/nile-laravel-server/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/c2143af46a7e3d1998d9cec06c79078a32c6d70c950aa8592cbe69c11960fe1b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a67616d626f612f6e696c652d6c61726176656c2d7365727665722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jgamboa/nile-laravel-server)

Nile Laravel Server
===================

[](#nile-laravel-server)

This package provides tools to support multi-tenancy in Laravel applications using PostgreSQL with context-based isolation \[thenile.dev\].

Support us
----------

[](#support-us)

\[[![](https://camo.githubusercontent.com/b55c4d00bdb4668f3827f596db356789d0beff3109188677214c8c59816ed7ec/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6e696c652d6c61726176656c2d7365727665722e6a70673f743d31)](https://camo.githubusercontent.com/b55c4d00bdb4668f3827f596db356789d0beff3109188677214c8c59816ed7ec/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6e696c652d6c61726176656c2d7365727665722e6a70673f743d31)\]

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require jgamboa/nile-laravel-server
```

You can publish and run the migrations with:

```
php artisan vendor:publish --tag="nile-laravel-server-migrations"
php artisan migrate
```

You can publish the config file with:

```
php artisan vendor:publish --tag="nile-laravel-server-config"
```

This is the contents of the published config file:

```
return [
];
```

or use the command install from the package: will copy config(beta), copy migrations and ask to run it:

```
php artisan nile-server:install
```

Optionally, you can publish the views using

```
php artisan vendor:publish --tag="nile-laravel-server-views"
```

Usage
-----

[](#usage)

### 1. Tenant Context Middleware: `NileContextMiddleware`

[](#1-tenant-context-middleware-nilecontextmiddleware)

This middleware reads the `x-nile-tenant-id` header and injects it into Laravel's application container using `App::instance('tenant_id', $id)`.

**Usage:**

```
use JGamboa\NileLaravelServer\Http\Middleware\NileContextMiddleware;

Route::middleware([NileContextMiddleware::class])->group(function () {
    // Routes protected by tenant context
});
```

### 2. Execute Queries in Tenant Context: TenantDB::run()

[](#2-execute-queries-in-tenant-context-tenantdbrun)

Use the TenantDB service to run queries that depend on the current tenant ID.

**Usage:**

```
use JGamboa\NileLaravelServer\Services\TenantDB;

TenantDB::run(function ($db) {
    return $db->table('users')->get();
});
```

This internally sets the tenant ID using: set local nile.tenant\_id = ?.

### 3. Multi-tenant Models: BelongsToTenant Trait

[](#3-multi-tenant-models-belongstotenant-trait)

Apply the BelongsToTenant trait to models that need to be scoped to a tenant.

**Usage:**

```
use JGamboa\NileLaravelServer\Traits\BelongsToTenant;

class BranchOffice extends Model
{
    use BelongsToTenant;
}
```

This trait adds a global scope and automatically fills the tenant\_id field during creation.

### 4. Tenant Model: Extend NileTenantModel

[](#4-tenant-model-extend-niletenantmodel)

You must create a model named Tenant that extends NileTenantModel. This provides support for UUIDs and custom timestamps.

**Usage:**

```
use JGamboa\NileLaravelServer\Models\NileTenantModel;

class Tenant extends NileTenantModel
{
    protected $table = 'tenants';
}
```

The base model includes:

- Automatic UUIDs
- Soft deletes
- Custom timestamp fields (`created`, `modified`, `deleted`)

### 4. User Model: Extend NileUserModel

[](#4-user-model-extend-nileusermodel)

You must modify laravel base User Model to extends NileUserModel. This provides support for UUIDs and custom timestamps.

**Usage:**

```
use JGamboa\NileLaravelServer\Models\NileUserModel;

class User extends NileUserModel
{
    // aquí puedes extender comportamiento o relaciones
}
```

The base model includes:

- Automatic UUIDs
- Soft deletes
- Custom timestamp fields (`created`, `modified`, `deleted`)
- `hasVerifiedEmail()` and `markEmailAsVerified()` functions adapted for Nile's `email_verified` column

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Joaquín Gamboa](https://github.com/JGamboa)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance52

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity17

Early-stage or recently created project

 Bus Factor1

Top contributor holds 71.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/ff2c9aaa69963da5b15e623c49d0702aa62275dadc246e01cfdc963c44e86a21?d=identicon)[JGamboa](/maintainers/JGamboa)

---

Top Contributors

[![JGamboa](https://avatars.githubusercontent.com/u/4482664?v=4)](https://github.com/JGamboa "JGamboa (10 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (2 commits)")

### Embed Badge

![Health badge](/badges/jgamboa-nile-laravel-server/health.svg)

```
[![Health](https://phpackages.com/badges/jgamboa-nile-laravel-server/health.svg)](https://phpackages.com/packages/jgamboa-nile-laravel-server)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
