PHPackages                             nunomazer/laravel-samehouse - 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. nunomazer/laravel-samehouse

ActiveLibrary

nunomazer/laravel-samehouse
===========================

A multi-tenant Laravel package, based on single database, simple and ease to use

1.4.0(1y ago)24239.0k↓15.5%11[1 issues](https://github.com/nunomazer/laravel-samehouse/issues)[1 PRs](https://github.com/nunomazer/laravel-samehouse/pulls)MITPHPPHP &gt;=5.6.0

Since May 5Pushed 1mo ago3 watchersCompare

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

READMEChangelog (5)Dependencies (4)Versions (6)Used By (0)

[![Latest Version on Packagist](https://camo.githubusercontent.com/fde41027ba0d959ecd2ee37a69125cb7b81d33ea4d0a79bc212152d558ee7f7a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e756e6f6d617a65722f6c61726176656c2d73616d65686f7573652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nunomazer/laravel-samehouse)[![Total Downloads](https://camo.githubusercontent.com/0ef265177bfba4deeffab8aeaf6d9bda3a57ed34654ebb9adcb22864a07f0f16/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e756e6f6d617a65722f6c61726176656c2d73616d65686f7573652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nunomazer/laravel-samehouse)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

laravel-samehouse
=================

[](#laravel-samehouse)

*for Laravel &amp; Lumen 5.2+*

A single database multi-tenancy package for Laravel &amp; Lumen version 5.2.

*NOTE: this package is based on HipsterJazzbo/Landlord and Torzer/awesome-landlord.**This version is a fork from  that was archived.*

Installation
------------

[](#installation)

To get started, require this package:

```
composer require nunomazer/laravel-samehouse
```

### Laravel

[](#laravel)

Add the ServiceProvider in `config/app.php`:

```
    'providers' => [
        ...
        NunoMazer\Samehouse\LandlordServiceProvider::class,
    ],
```

Register the Facade if you’d like:

```
    'aliases' => [
        ...
        'Landlord'   => NunoMazer\Samehouse\Facades\Landlord::class,
    ],
```

You could also publish the config file:

```
php artisan vendor:publish --provider="NunoMazer\Samehouse\LandlordServiceProvider"
```

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

[](#lumen)

You'll need to set the service provider in your `bootstrap/app.php`:

```
$app->register(NunoMazer\Samehouse\LandlordServiceProvider::class);
```

And make sure you've un-commented `$app->withEloquent()`.

Usage
-----

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

[](#adding-and-removing-tenants)

> **IMPORTANT NOTE:** Laravel Samehouse 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:

```
Landlord::addTenant('tenant_id', 1);
```

Or an instance of a Tenant model:

```
$tenant = Tenant::find(1);

Landlord::addTenant($tenant);
```

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.

Following is a sample middleware to implement the process of setting tenant on every request.

First create the class:

```
php artisan make:middleware SetTenant
```

Then, code it to set the tenant id for authenticated user, you can user the following example:

```
