{"id":19613352,"url":"https://github.com/solutosoft/laravel-multitenant","last_synced_at":"2025-04-28T00:31:27.936Z","repository":{"id":57055419,"uuid":"171878395","full_name":"solutosoft/laravel-multitenant","owner":"solutosoft","description":"Single database multi-tenancy solution for Laravel","archived":false,"fork":false,"pushed_at":"2024-03-20T18:23:23.000Z","size":126,"stargazers_count":24,"open_issues_count":1,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-09-28T05:15:35.560Z","etag":null,"topics":["eloquent","laravel","laravel-package","multi-tenant","tenancy","tenant"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/solutosoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-21T13:38:11.000Z","updated_at":"2024-06-04T11:43:59.000Z","dependencies_parsed_at":"2023-02-17T12:16:08.101Z","dependency_job_id":null,"html_url":"https://github.com/solutosoft/laravel-multitenant","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solutosoft%2Flaravel-multitenant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solutosoft%2Flaravel-multitenant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solutosoft%2Flaravel-multitenant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/solutosoft%2Flaravel-multitenant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/solutosoft","download_url":"https://codeload.github.com/solutosoft/laravel-multitenant/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224092058,"owners_count":17254152,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","laravel","laravel-package","multi-tenant","tenancy","tenant"],"created_at":"2024-11-11T10:48:03.586Z","updated_at":"2024-11-11T10:48:56.304Z","avatar_url":"https://github.com/solutosoft.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Multi Tenancy\n\n[![Build Status](https://github.com/solutosoft/laravel-multitenant/actions/workflows/tests.yml/badge.svg)](https://github.com/solutosoft/laravel-multitenant/actions)\n[![Total Downloads](https://poser.pugx.org/solutosoft/laravel-multitenant/downloads.png)](https://packagist.org/packages/solutosoft/multitenant)\n[![Latest Stable Version](https://poser.pugx.org/solutosoft/laravel-multitenant/v/stable.png)](https://packagist.org/packages/solutosoft/multitenant)\n\nThe **Shared Database** used by all tenants, means that we keep data from all the tenants in the same database. To isolate tenant specific data, we will have to add a discriminator column like `tenant_id` to every table which is tenant specific, and to make sure that all the queries and commands will filter the data based on it.\n\nWith this strategy dealing with tenant shared data is simple, we just don't filter it. Isolating data is what we need to deal with. For this we need to make sure that ALL the queries and the commands that deal with tenant specific data get filtered by the `tenant_id`.\n\nThis extension allows control the Eloquent with shared database used by all tenants.\n\nInstallation\n------------\n\nThe preferred way to install this library is through composer.\n\nEither run\n\n`composer require --prefer-dist solutosoft/laravel-multitenant \"*\"`\n\nor add\n\n`\"solutosoft/laravel-multitenant\": \"*\"`\n\nto the require section of your composer.json.\n\nUsage\n-----\n\n1. Create table with `tenant_id` column:\n\n```php\n/**\n * Run the migrations.\n *\n * @return void\n  */\npublic function up()\n{\n    Schema::create('users', function (Blueprint $table) {\n        $table-\u003eincrements('id');\n        $table-\u003estring('firstName');\n        $table-\u003estring('lastName');\n        $table-\u003estring('login')-\u003enullable();\n        $table-\u003estring('password')-\u003enullable();\n        $table-\u003estring('remember_token')-\u003enullable();\n        $table-\u003estring('active');\n        $table-\u003einteger('tenant_id')-\u003enullable(true);\n    });\n\n    $Schema::create('pets', function (Blueprint $table) {\n        $table-\u003eincrements('id');\n        $table-\u003estring('name');\n        $table-\u003einteger('tenant_id');\n    });\n}\n```\n\n2. Uses the `MultiTenant` trait, it add a [Global Scope](https://laravel.com/docs/5.7/eloquent#global-scope) filtering\nany query by `tenant_id` column.\n\n```php\n\n\u003c?php\n\nuse Illuminate\\Auth\\Authenticatable;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Solutosoft\\MultiTenant\\MultiTenant;\nuse Solutosoft\\MultiTenant\\Tenant;\n\nclass User extends Model implements Tenant\n{\n    use MultiTenant, Authenticatable;\n\n    /**\n     * @inheritdoc\n     */\n    public function getTenantId()\n    {\n        return $this-\u003etenant_id;\n    }\n\n    ...\n}\n\nclass Pet extends Model\n{\n\n  use MultiTenant;\n\n  ...\n\n}\n\n```\n\nNow when you save or execute same query the `tenant_id` column will be used. Example:\n\n```php\n// It's necessary will be logged in\n\n$users = App\\User::where('active', 1)-\u003eget();\n// select * from `users` where `active` = 1 and tenant_id = 1\n\n$pet = Pet::create(['name' =\u003e 'Bob']);\n// insert into `pet` (`name`, 'tenant_id') values ('Bob', 1)\n```\n\nAuth Service Provider\n---------------------\n\nIt's necessary change the authentication service provider:\n\n1. Creates new file: `app/Providers/TenantUserProvider.php`\n\n```php\n\n\u003c?php\n\nnamespace App\\Providers;\n\nuse Illuminate\\Auth\\EloquentUserProvider;\nuse Solutosoft\\MultiTenant\\TenantScope;\n\nclass TenantUserProvider extends EloquentUserProvider\n{\n\n    protected function newModelQuery($model = null)\n    {\n        return parent::newModelQuery($model)-\u003ewithoutGlobalScope(TenantScope::class);\n    }\n\n}\n```\n\n2. Edit `app/Providers/AuthServiceProvider.php`\n\n```php\n\n\u003c?php\n\nnamespace App\\Providers;\n\nclass AuthServiceProvider extends ServiceProvider\n{\n    ...\n\n    /**\n     * Register any authentication / authorization services.\n     *\n     * @return void\n     */\n    public function boot()\n    {\n        ...\n\n        Auth::provider('multitenant', function($app, $config) {\n            return new TenantUserProvider($app['hash'], $config['model']);\n        });\n    }\n}\n```\n\n3. Edit `config/auth.php`\n\n```php\n\nreturn [\n    ....\n    'providers' =\u003e [\n        'users' =\u003e [\n            'driver' =\u003e 'multitenant',\n            'model' =\u003e App\\Models\\User::class,\n        ],\n    ],\n\n    ...\n```\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsolutosoft%2Flaravel-multitenant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsolutosoft%2Flaravel-multitenant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsolutosoft%2Flaravel-multitenant/lists"}