Download the PHP package jeffersonsimaogoncalves/multitenancy without Composer

On this page you can find all versions of the php package jeffersonsimaogoncalves/multitenancy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package multitenancy

Multitenancy Package

Total Downloads

This package is meant to be a quick and easy way to add multitenancy to your Laravel application. It simply creates models and relationships for Tenants and models. The package identifies incoming traffic by subdomain, and finds a corresponding tenant in the Tenant table. If none are found or the user is not associated with a particular subdomain, the user is met with a 403 error.

The admin subdomain is reserved for the package. It is used to automatically remove all scopes from users with a Super Administrator role.

To scope a resource to the currently accessed subdomain, you simply need to add a foreign key relationship to the model's table. The package middleware will automatically apply the scopes for the relevant models.

Any resources saved while accessing a scoped subdomain will automatically be saved against the current tenant, based on subdomain.

Installation

You can install the package via composer:

In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework you should add the service provider in the config/app.php file:

You can publish the config file with:

If you want the user to automatically be assigned to the Tenant it is created on you can enable this functionality by simply enabling the ignore_tenant_on_user_creation setting.

You can automate most of the setup by running:

It will:

Usage

First, add the JeffersonSimaoGoncalves\Multitenancy\Traits\HasTenants and Spatie\Permission\Traits\HasRoles traits to your User model(s):

The package relies on Eloquent, so you may access the User's tenants with User::tenants()->get().

Inversely, you may access the Tenant's users with Tenant::users()->get().

Tenants require a name to identify the tenant and and a subdomain that is associated with that user. Example:

tenant1.example.com

tenant2.example.com

These values could be added to the database like so:

You can then attach user models to the Tenant:

Middleware

Authenticated

This package comes with TenantMiddleware middleware which extends Laravel's Illuminate\Auth\Middleware\Authenticate. You can add it inside your app/Http/Kernel.php file.

Then you can bring multitenancy to your routes using middleware rules:

Guest Tenant

This package comes with GuestTenantMiddleware middleware which applies the tenant scope to all models and can be used for allowing guest users to access Tenant related pages. You can add it inside your app/Http/Kernel.php file.

Then you can bring multitenancy to your routes using middleware rules:

Tenant Assignment for Models

Models can automatically inherit scoping of the current tenant by adding a trait and migration to a model. This would allow users to access tenant1.example.com and return the data from tenant1 only.

For example, say you wanted Tenants to manage their own Product. In your Product model, add the BelongsToTenant trait. Then run the provided console command to add the necessary relationship column to your existing products table.

Hint If the user is assigned Super Administrator access, they will be able to access the admin subdomain and the tenant scope will not register. This allows you to manage the data across all instances without needing individual access to each Tenant's account.

Get Current Tenant

You can get the current tenant model:

Providing Access to Admin Domain

In order to access the admin.example.com subdomain, a user will need the access admin permission. This package relies on Spatie's Laravel Permission package and is automatically included as a dependency when installing this package. We also provide a Super Administrator role on migration that has the relevant permission already associated with it. Assign the Super Administrator role to an admin user to provide the access they need. See the Laravel Permission documentation for more on adding users to the appropriate role and permission.

A subdomain for the admin portal will be created automatically during installation, but you can manually add it like this:

Console Commands

Generate a migration to add tenancy to an existing model's table:

Assign a user Super Administration rights and the admin Tenant:

Managing with Nova

There is a separate Nova Package available that allows you to manage the resources utilized in this package in Nova.

Reporting Issues

If you have a problem with this plugin or any bug, please open an issue on GitHub.

Credits

This work is based on the code by RomegaDigital.


All versions of multitenancy with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4|^8.0
spatie/laravel-permission Version ^3.0|^4.0|^5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package jeffersonsimaogoncalves/multitenancy contains the following files

Loading the files please wait ....