<commit_before># Landlord for Laravel & Lumen 5.2+

![Landlord for Laravel & Lumen 5.2+](readme-header.jpg)

![StyleCI Status](https://styleci.io/repos/49851417/shield?branch=v2.0-wip)  
![Build Status](https://travis-ci.org/HipsterJazzbo/Landlord.svg?branch=v2.0-wip)

A single database multi-tenancy package for Laravel & Lumen 5.2+.

> **Upgrading from Landlord v1?** Make sure to read the [change log](CHANGELOG.md) to see what needs updating.

## Installation

To get started, require this package:

```bash
composer require hipsterjazzbo/landlord
```

### Laravel

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

```php
    'providers' => [
        ...
        HipsterJazzbo\Landlord\LandlordServiceProvider::class,
    ],
```

Register the Facade if you’d like:

```php
    'aliases' => [
        ...
        'Landlord'   => HipsterJazzbo\Landlord\Facades\Landlord::class,
    ],
```

You could also publish the config file:

```bash
php artisan vendor:publish --provider="HipsterJazzbo\Landlord\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

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

```php
$app->register(HipsterJazzbo\Landlord\LandlordServiceProvider::class);
```

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

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:

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

Or an instance of a Tenant model:

```php
$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.

To remove a tenant and stop scoping by it, simply call `removeTenant()`:

```php
Landlord::removeTenant('tenant_id');

// Or you can again pass a Model instance:
$tenant = Tenant::find(1);

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

You can also check whether Landlord currently is scoping by a given tenant:

```php
// As you would expect by now, $tenant can be either a string column name or a Model instance
Landlord::hasTenant($tenant);
```

And if for some reason you need to, you can retrieve Landlord's tenants:

```php
// $tenants is a Laravel Collection object, in the format 'tenant_id' => 1
$tenants = Landlord::getTenants();
```

> **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.

### Setting up your Models

To set up a model to be scoped automatically, simply use the `BelongsToTenants` trait:
<commit_msg>Move note about statelessness<commit_after># Landlord for Laravel & Lumen 5.2+

![Landlord for Laravel & Lumen 5.2+](readme-header.jpg)

![StyleCI Status](https://styleci.io/repos/49851417/shield?branch=v2.0-wip)  
![Build Status](https://travis-ci.org/HipsterJazzbo/Landlord.svg?branch=v2.0-wip)

A single database multi-tenancy package for Laravel & Lumen 5.2+.

> **Upgrading from Landlord v1?** Make sure to read the [change log](CHANGELOG.md) to see what needs updating.

## Installation

To get started, require this package:

```bash
composer require hipsterjazzbo/landlord
```

### Laravel

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

```php
    'providers' => [
        ...
        HipsterJazzbo\Landlord\LandlordServiceProvider::class,
    ],
```

Register the Facade if you’d like:

```php
    'aliases' => [
        ...
        'Landlord'   => HipsterJazzbo\Landlord\Facades\Landlord::class,
    ],
```

You could also publish the config file:

```bash
php artisan vendor:publish --provider="HipsterJazzbo\Landlord\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

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

```php
$app->register(HipsterJazzbo\Landlord\LandlordServiceProvider::class);
```

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:

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

Or an instance of a Tenant model:

```php
$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.

To remove a tenant and stop scoping by it, simply call `removeTenant()`:

```php
Landlord::removeTenant('tenant_id');

// Or you can again pass a Model instance:
$tenant = Tenant::find(1);

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

You can also check whether Landlord currently is scoping by a given tenant:

```php
// As you would expect by now, $tenant can be either a string column name or a Model instance
Landlord::hasTenant($tenant);
```

And if for some reason you need to, you can retrieve Landlord's tenants:

```php
// $tenants is a Laravel Collection object, in the format 'tenant_id' => 1
$tenants = Landlord::getTenants();
```

### Setting up your Models

To set up a model to be scoped automatically, simply use the `BelongsToTenants` trait:

{'fraction_non_alphanumeric': 0.10751445086705202, 'fraction_numerical': 0.006647398843930636, 'mean_word_length': 4.466824644549763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3121665', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2107, 'n_words': 859}