jgamboa / nile-laravel-server
Package to connect to nile.dev PG Multi tenant
Fund package maintenance!
JGamboa
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- illuminate/support: ^10.0||^11.0||^12.0
- laravel/sanctum: ^4.0
- spatie/laravel-package-tools: ^1.92
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
This package is auto-updated.
Last update: 2025-06-16 00:23:47 UTC
README
Nile Laravel Server
This package provides tools to support multi-tenancy in Laravel applications using PostgreSQL with context-based isolation [thenile.dev].
Support us
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
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()
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
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
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
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()
andmarkEmailAsVerified()
functions adapted for Nile'semail_verified
column
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.