Download the PHP package danthedj/multitenant without Composer
On this page you can find all versions of the php package danthedj/multitenant. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download danthedj/multitenant
More information about danthedj/multitenant
Files in danthedj/multitenant
Package multitenant
Short Description Laravel Tenant Dynamic Database Solution.
License MIT
Informations about the package multitenant
Laravel MultiTenant 1.4
The Laravel 5.4 MultiTenant package enables you to easily add multi-tenant capabilities to your application. This package is designed using a minimalist approach.Just drop it in, run the migration, and start adding tenants. Your applications will have access to current tenant information through the dynamically set config('tenant') values. Optionally, you can let applications reconnect to the default master database so a tenant could manage all tenant other accounts for example. And, perhaps the best part, Artisan is completely multi-tenant aware! Just add the --tenant option to any command to run that command on one or all tenants. Works on migrations, queueing, etc.!
MultiTenant also offers a TenantContract, triggers Laravel events, throws a TenantNotResolvedException and TenantDatabaseNameEmptyException, so you can easily add in custom functionality and tweak it for your needs.
Laravel MultiTenant was forked from @thinksaydo, who modified the original Tenantable project by @leemason. All of the main code is due to them. The difference in this project is that it allows for a database per tenant, compared to a single database with table prefixes. This allows for a more managed approach in some cases.
MultiTenant relies on your ENV and Database config and stores just the connection name in the table and only allows one subdomain and domain per tenant, which is most often plenty for most apps. MultiTenant also throws a TenantNotResolvedException when tenants are not found, and a TenantDatabaseNameEmptyException when the database name could not be determined.
Simple Installation & Usage
Composer install:
Generate composer autoload file:
Tenants database table install (uses default database connection):
Service provider install:
Resolve every route/request
If you want to resolve the client on every route/request add in
to the Service providers array in config/app.php
Note: This is the default that Laravel 5.5 (and above) will use for auto discovery. If you want to use the middleware option please add the following to your project's composer.json file.
Resolve tenants through Middleware
If you only want to resolve tenant using a Middleware on a route add in
to the Service providers array in config/app.php
instead of the service provider above.
Database connection:
In config/database.php
create a new connection. For the host
, port
,username
and password
, these are picked up from the .env
file.
Tenant creation (just uses a standard Eloquent model):
And you're done! Minimalist, simple. Whenever your app is visited via http://acme.domain.com or http://acmeinc.com the connection "tenant_db" will be used, the database name will switch to "{prefix}_acme", and config('tenant') will be set with tenant details allowing you to access values from your views or application.
Advanced EnvTenant Usage
Artisan
The --tenant option works on all Artisan commands:
You can also use the --tenant option when tinkering:
Tenant
The class is a simple Eloquent model providing basic tenant settings.
TenantResolver
The class is responsible for resolving and managing the active tenant during Web and Artisan access. You can access the resolver class using .
If you want to use a custom model, register a custom service provider that binds a singleton to the TenantContract and resolves to an instance of your custom tenant model. EnvTenant will automatically defer to your custom model as long as you load your service provider before loading the EnvTenant\TenantServiceProvider.
Create this example service provider in your app/Providers folder as CustomTenantServiceProvider.php:
Then register in your config/app.php file.
Events
Throughout the lifecycle events are fired allowing you to listen and customize behavior.
Tenant activated:
Tenant resolved:
Tenant not resolved:
Tenant not resolved via the Web, an exception is thrown:
Tenant database name not be determined or empty: