PHPackages                             sereny/laravel-multicompany - 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. sereny/laravel-multicompany

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

sereny/laravel-multicompany
===========================

Multi company for eloquent models

05PHP

Since May 21Pushed 1y ago1 watchersCompare

[ Source](https://github.com/serenysoft/laravel-multicompany)[ Packagist](https://packagist.org/packages/sereny/laravel-multicompany)[ RSS](/packages/sereny-laravel-multicompany/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Multi Company
=====================

[](#laravel-multi-company)

[![Build Status](https://github.com/serenysoft/laravel-multicompany/actions/workflows/tests.yml/badge.svg)](https://github.com/serenysoft/laravel-multicompany/actions/workflows/tests.yml)[![Total Downloads](https://camo.githubusercontent.com/96d358b11563987276bbe3298c534942c2641d8fb8a45b501cd87787514d3cfb/68747470733a2f2f706f7365722e707567782e6f72672f736572656e792f6c61726176656c2d6d756c7469636f6d70616e792f646f776e6c6f6164732e706e67)](https://packagist.org/packages/sereny/laravel-multicompany)[![Latest Stable Version](https://camo.githubusercontent.com/e382a4d6a9c70e8826d8d41bc838be4396173d28fa85f54e220f7f194a20d805/68747470733a2f2f706f7365722e707567782e6f72672f736572656e792f6c61726176656c2d6d756c7469636f6d70616e792f762f737461626c652e706e67)](https://packagist.org/packages/sereny/laravel-multicompany)

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

[](#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
------------

[](#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
-----

[](#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();
    });
}
```

2. Set `\Sereny\MultiCompany\Middleware\InitializeCompanyByRequestData::class` as middleware
3. Uses the `InteractsWithCompany` trait, it add a [Global Scope](https://laravel.com/docs/master/eloquent#global-scopes)filtering any query by `company_id` column.

```
