定制 jgamboa/nile-laravel-server 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jgamboa/nile-laravel-server

Composer 安装命令:

composer require jgamboa/nile-laravel-server

包简介

Package to connect to nile.dev PG Multi tenant

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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() and markEmailAsVerified() functions adapted for Nile's email_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.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-07