Download the PHP package betalabs/landlord without Composer
On this page you can find all versions of the php package betalabs/landlord. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download betalabs/landlord
More information about betalabs/landlord
Files in betalabs/landlord
Package landlord
Short Description A simple, single database multi-tenancy solution for Laravel 5.2+
License MIT
Informations about the package landlord
Landlord for Laravel & Lumen 5.2+
Based on HipsterJazzbo/Landlord, a single database multi-tenancy package for Laravel & Lumen 5.2+.
Installation
To get started, require this package:
Laravel
Add the ServiceProvider in config/app.php
:
Register the Facade if you’d like:
You could also publish the config file:
and set your default_tenant_columns
setting, if you have an app-wide default. LandLord will use this setting to scope models that don’t have a $tenantColumns
property set.
Lumen
You'll need to set the service provider in your bootstrap/app.php
:
And make sure you've un-commented $app->withEloquent()
.
Usage
This package assumes that you have at least one column on all of your Tenant scoped tables that references which tenant each row belongs to.
For example, you might have a companies
table, and a bunch of other tables that have a company_id
column.
Adding and Removing Tenants
IMPORTANT NOTE: Landlord is stateless. This means that when you call
addTenant()
, it will only scope the current request.Make sure that you are adding your tenants in such a way that it happens on every request, and before you need Models scoped, like in a middleware or as part of a stateless authentication method like OAuth.
You can tell Landlord to automatically scope by a given Tenant by calling addTenant()
, either from the Landlord
facade, or by injecting an instance of TenantManager()
.
You can pass in either a tenant column and id:
Or an instance of a Tenant model:
If you pass a Model instance, Landlord will use Eloquent’s getForeignKey()
method to decide the tenant column name.
You can add as many tenants as you need to, however Landlord will only allow one of each type of tenant at a time.
To remove a tenant and stop scoping by it, simply call removeTenant()
:
You can also check whether Landlord currently is scoping by a given tenant:
And if for some reason you need to, you can retrieve Landlord's tenants:
Setting up your Models
To set up a model to be scoped automatically, simply use the BelongsToTenants
trait:
If you’d like to override the tenants that apply to a particular model, you can set the $tenantColumns
property:
Creating new Tenant scoped Models
When you create a new instance of a Model which uses BelongsToTenants
, Landlord will automatically add any applicable Tenant ids, if they are not already set:
Querying Tenant scoped Models
After you've added tenants, all queries against a Model which uses BelongsToTenant
will be scoped automatically:
Note: When you are developing a multi tenanted application, it can be confusing sometimes why you keep getting
ModelNotFound
exceptions for rows that DO exist, because they belong to the wrong tenant.Landlord will catch those exceptions, and re-throw them as
ModelNotFoundForTenantException
, to help you out :)
If you need to query across all tenants, you can use allTenants()
:
Under the hood, Landlord uses Laravel's anonymous global scopes. This means that if you are scoping by multiple tenants simultaneously, and you want to exclude one of the for a single query, you can do so:
Contributing
If you find an issue, or have a better way to do something, feel free to open an issue or a pull request.
All versions of landlord with dependencies
illuminate/support Version 5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0