Using a custom tenant model | laravel-multitenancy | Spatie

 SPATIE

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-multitenancy](https://spatie.be/docs/laravel-multitenancy/v4)  Advanced-usage  Using a custom tenant model

 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/v4/introduction)
- [ Upgrade guide ](https://spatie.be/docs/laravel-multitenancy/v4/upgrade-guide)
- [ Support us ](https://spatie.be/docs/laravel-multitenancy/v4/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-multitenancy/v4/requirements)
- [ Questions and issues ](https://spatie.be/docs/laravel-multitenancy/v4/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-multitenancy/v4/changelog)
- [ ](https://spatie.be/docs/laravel-multitenancy/v4/installation-setup)

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

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

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

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

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

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

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

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

 Using a custom tenant model
===========================

###  On this page

1. [ Option 1: extending the ](#content-option-1-extending-the-tenant-model-provided-by-the-package)
2. [ Option 2: using a model of your own ](#content-option-2-using-a-model-of-your-own)
3. [ Performing actions when a tenant gets created ](#content-performing-actions-when-a-tenant-gets-created)

If you want to change or add behaviour on the `Tenant` model you can use your custom model. There are two ways of doing this by extending the `Tenant` model provided by the package, or by prepping a model of your own.

Option 1: extending the `Tenant` model provided by the package
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Make sure that your custom model extends the `Spatie\Multitenancy\Models\Tenant` model provided by the package.

You should specify the class name of your model in the `tenant_model` key of the `multitenancy` config file.

```
/*
 * This class is the model used for storing configuration on tenants.
 *
 * It must be or extend `Spatie\Multitenancy\Models\Tenant::class`
 */
'tenant_model' => \App\Models\CustomTenantModel::class,
```

Option 2: using a model of your own
---------------------------------------------------------------------------------------------------------------------------------------------------------------

You don't have to extend our `Tenant` model. For example if you use Laravel Jetstream, then you probably want to use `Team` model provided by that package as your tenant model.

To accomplish that, you can implement the `IsTenant` interface and use trait `ImplementsTenant` to fulfill that interface.

Here's an example:

```
namespace App\Models;

use Laravel\Jetstream\Team as JetstreamTeam;
use Spatie\Multitenancy\Contracts\IsTenant;
use Spatie\Multitenancy\Models\Concerns\ImplementsTenant;

class Team extends JetstreamTeam implements IsTenant
{
    use HasFactory;
    use UsesLandlordConnection;
    use ImplementsTenant;
}
```

Performing actions when a tenant gets created
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You can leverage Eloquent's lifecycle callbacks to execute extra logic when a tenant gets created, updated, deleted, ...

Here's an example on how you could call some logic that creates a database when a tenant gets created.

```
namespace App\Models\Tenant;

use Spatie\Multitenancy\Models\Tenant;

class CustomTenantModel extends Tenant
{
    protected static function booted()
    {
        static::creating(fn(CustomTenantModel $model) => $model->createDatabase());
    }

    public function createDatabase()
    {
        // add logic to create database
    }
}
```
