Working with the current tenant | laravel-multitenancy | Spatie

 SPATIE

  Laravel Multitenancy
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-multitenancy](https://spatie.be/docs/laravel-multitenancy/v2)  Basic-usage  Working with the current tenant

 Version   v4   v3   v2   v1

 Other versions for crawler [v4](https://spatie.be/docs/laravel-multitenancy/v4) [v3](https://spatie.be/docs/laravel-multitenancy/v3) [v2](https://spatie.be/docs/laravel-multitenancy/v2) [v1](https://spatie.be/docs/laravel-multitenancy/v1)

- [ Introduction ](https://spatie.be/docs/laravel-multitenancy/v2/introduction)
- [ Support us ](https://spatie.be/docs/laravel-multitenancy/v2/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-multitenancy/v2/requirements)
- [ Questions and issues ](https://spatie.be/docs/laravel-multitenancy/v2/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-multitenancy/v2/changelog)
- [ ](https://spatie.be/docs/laravel-multitenancy/v2/installation-setup)

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

- [ Base installation ](https://spatie.be/docs/laravel-multitenancy/v2/installation/base-installation)
- [ Using a single database ](https://spatie.be/docs/laravel-multitenancy/v2/installation/using-a-single-database)
- [ Using multiple databases ](https://spatie.be/docs/laravel-multitenancy/v2/installation/using-multiple-databases)
- [ Determining the current tenant ](https://spatie.be/docs/laravel-multitenancy/v2/installation/determining-current-tenant)

Basic usage
-----------

- [ Automatically determining the current tenant ](https://spatie.be/docs/laravel-multitenancy/v2/basic-usage/automatically-determining-the-current-tenant)
- [ Working with the current tenant ](https://spatie.be/docs/laravel-multitenancy/v2/basic-usage/working-with-the-current-tenant)
- [ Making queues tenant aware ](https://spatie.be/docs/laravel-multitenancy/v2/basic-usage/making-queues-tenant-aware)

Advanced usage
--------------

- [ Ensuring a current tenant has been set ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/ensuring-a-current-tenant-has-been-set)
- [ Looping over a collection of tenants ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/looping-over-a-collection-of-tenants)
- [ Making Artisan command tenant aware ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/making-artisan-commands-tenant-aware)
- [ Using a custom tenant model ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/using-a-custom-tenant-model)
- [ Listening for events ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/listening-for-events)
- [ Using tenant specific facades ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/using-tenant-specific-facades)
- [ Executing code for tenants and landlords ](https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/executing-code-for-tenants-and-landlords)

Using tasks to prepare the environment
--------------------------------------

- [ Overview ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/overview)
- [ Creating your own task ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/creating-your-own-task)
- [ Switching databases ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/switching-databases)
- [ Switching route cache paths ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/switching-route-cache-paths)
- [ Prefixing cache ](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/prefixing-cache)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-multitenancy                                                                                                                                                                                                                                    `

Working with the current tenant
===============================

There are several methods available to get, set and clear the current tenant.

### Finding the current tenant

You can find the current tenant like this:

```
Spatie\Multitenancy\Models\Tenant::current(); // returns the current tenant, or if no tenant is current, `null`
```

A current tenant will also be bound in the container using the `currentTenant` key.

```
app('currentTenant'); // returns the current tenant, or if no tenant is current, `null`
```

### Checking if there is a current tenant

You can check if there is tenant set as the current one:

```
Tenant::checkCurrent() // returns `true` or `false`
```

### Manually setting the current tenant

You can manually make a tenant the current one by calling `makeCurrent()` on it.

```
$tenant->makeCurrent();
```

When a tenant is made the current one, the package will run the `makeCurrent` method of [all tasks configured](/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/overview/) in the `switch_tenant_tasks` key of the `multitenancy` config file.

### Forgetting the current tenant

You can forget the current tenant:

```
Tenant::forgetCurrent();

Tenant::current() // return null;
```

If there was no tenant current when calling `forgetCurrent`, the function will do nothing.
