sereny/laravel-multicompany

Multi company for eloquent models

dev-master / 1.0.x-dev 2024-05-21 12:10 UTC

This package is auto-updated.

Last update: 2024-11-21 13:09:00 UTC


README

Build Status Total Downloads Latest Stable Version

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_id on 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

  1. Create table with tenant_id column:
/**
 * 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();
    });
}
  1. Set \Sereny\MultiCompany\Middleware\InitializeCompanyByRequestData::class as middleware

  2. Uses the InteractsWithCompany trait, it add a Global Scope filtering any query by company_id column.

<?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)