sereny/laravel-multicompany
Composer 安装命令:
composer require sereny/laravel-multicompany
包简介
Multi company for eloquent models
README 文档
README
This Laravel library provides a flexible and secure approach to multicompany within a shared database. It allows you to filter and populate the company_id field of models that belong to a company, enabling you to manage data for multiple companies while maintaining data separation.
Features
- Model Scopes: Filter models based on the currently authenticated company, ensuring data isolation.
- Middleware: Automatically set the
company_idon incoming requests based on a configurable identifier. - Company Detection: Easily identify the current company within your application.
Installation
The preferred way to install this library is through composer.
Either run
composer require --prefer-dist sereny/laravel-multicompany "*"
or add
"sereny/laravel-multicompany": "*"
to the require section of your composer.json.
Usage
- Create table with
tenant_idcolumn:
/** * Run the migrations. * * @return void */ public function up() { Schema::create('companies', function (Blueprint $table) { $table->increments('id'); $table->string('name'); }); Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('login')->nullable(); $table->string('password')->nullable(); $table->string('remember_token')->nullable(); $table->foreignId('company_id')->constrained(); }); }
-
Set
\Sereny\MultiCompany\Middleware\InitializeCompanyByRequestData::classas middleware -
Uses the
InteractsWithCompanytrait, it add a Global Scope filtering any query bycompany_idcolumn.
<?php use Illuminate\Database\Eloquent\Model; use Sereny\MultiCompany\InteractsWithCompany; use Sereny\MultiCompany\Tenant; class User extends Model implements Tenant { use InteractsWithCompany, Authenticatable; ... }
Now when you save or execute same query the company_id column will be used. Example:
// It's necessary will be logged in User::where('active', 1)->get(); // select * from `users` where `active` = 1 and company_id = 1 User::create(['name' => 'Bob']); // insert into `pet` (`name`, 'company_id') values ('Bob', 1)
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2024-05-19