Skip to content

Commit

Permalink
prep for v4
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jul 24, 2024
1 parent 8a22870 commit a9d125c
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/_index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: v3
title: v4
slogan: An unopinionated multitenancy package for Laravel apps
githubUrl: https://github.com/spatie/laravel-multitenancy
branch: main
Expand Down
8 changes: 4 additions & 4 deletions docs/advanced-usage/listening-for-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ The package fires events where you can listen for to perform some extra logic.

## `\Spatie\Multitenancy\Events\MakingTenantCurrentEvent`

This event will fire when a tenant is being made the current one. At this point none of [the tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) have been executed.
This event will fire when a tenant is being made the current one. At this point none of [the tasks](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/overview/) have been executed.

It has one public property `$tenant`, that contains an instance of `Spatie\Multitenancy\Models\Tenant`.

## `\Spatie\Multitenancy\Events\MadeTenantCurrentEvent`

This event will fire when a tenant has been made the current one. At this point the `makeCurrent` method of all of [the tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) have been executed. The current tenant also have been bound as `currentTenant` in the container.
This event will fire when a tenant has been made the current one. At this point the `makeCurrent` method of all of [the tasks](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/overview/) have been executed. The current tenant also have been bound as `currentTenant` in the container.

It has one public property `$tenant`, that contains an instance of `Spatie\Multitenancy\Models\Tenant`.

## `\Spatie\Multitenancy\Events\ForgettingCurrentTenantEvent`

This event will fire when a tenant is being forgotten. At this point none of [the tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) have been executed.
This event will fire when a tenant is being forgotten. At this point none of [the tasks](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/overview/) have been executed.

It has one public property `$tenant`, that contains an instance of `Spatie\Multitenancy\Models\Tenant`.

## `\Spatie\Multitenancy\Events\ForgotCurrentTenantEvent`

This event will fire when a tenant has been forgotten. At this point the `forgotCurrent` method of all of [the tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) have been executed. `currentTenant` in the container has been emptied.
This event will fire when a tenant has been forgotten. At this point the `forgotCurrent` method of all of [the tasks](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/overview/) have been executed. `currentTenant` in the container has been emptied.

## `\Spatie\Multitenancy\Events\TenantNotFoundForRequestEvent`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ php artisan your-favorite-command --tenant=1

If you cannot change an Artisan command yourself, for instance a command from Laravel itself or a command from a package, you can use `tenants:artisan <artisan command>`. This command will loop over tenants and for each of them make that tenant current, and execute the artisan command.

When your tenants each have their own database, you could migrate each tenant database with this command (given you are using a task like [`SwitchTenantDatabase`](https://docs.spatie.be/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/switching-databases)):
When your tenants each have their own database, you could migrate each tenant database with this command (given you are using a task like [`SwitchTenantDatabase`](https://docs.spatie.be/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/switching-databases)):

```bash
php artisan tenants:artisan migrate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ In the `multitenancy` config file, you specify the tenant finder in the `tenant_
'tenant_finder' => Spatie\Multitenancy\TenantFinder\DomainTenantFinder::class,
```

If there is a tenant returned by the tenant finder, [all configured tasks](https://docs.spatie.be/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) will be performed on it. After that, the tenant instance will be bound in the container using the `currentTenant` key.
If there is a tenant returned by the tenant finder, [all configured tasks](https://docs.spatie.be/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/overview/) will be performed on it. After that, the tenant instance will be bound in the container using the `currentTenant` key.

```php
app('currentTenant') // will return the current tenant or `null`
Expand Down
2 changes: 1 addition & 1 deletion docs/basic-usage/working-with-the-current-tenant.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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/v3/using-tasks-to-prepare-the-environment/overview/) in the `switch_tenant_tasks` key of the `multitenancy` config file.
When a tenant is made the current one, the package will run the `makeCurrent` method of [all tasks configured](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/overview/) in the `switch_tenant_tasks` key of the `multitenancy` config file.

### Forgetting the current tenant

Expand Down
6 changes: 3 additions & 3 deletions docs/installation/base-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ Route::middleware('tenant')->group(function() {
});
```

This middleware will respond with an unauthorized response code (401) when the user tries to use their session to view another tenant. Make sure to include `\Spatie\Multitenancy\Http\Middleware\NeedsTenant` first, as this will [handle any cases where a valid tenant cannot be found](/docs/laravel-multitenancy/v3/advanced-usage/ensuring-a-current-tenant-has-been-set).
This middleware will respond with an unauthorized response code (401) when the user tries to use their session to view another tenant. Make sure to include `\Spatie\Multitenancy\Http\Middleware\NeedsTenant` first, as this will [handle any cases where a valid tenant cannot be found](/docs/laravel-multitenancy/v4/advanced-usage/ensuring-a-current-tenant-has-been-set).

### Next steps

If you prefer to use just one glorious database for all your tenants, read the installation instructions for [using a single database](/docs/laravel-multitenancy/v3/installation/using-a-single-database).
If you prefer to use just one glorious database for all your tenants, read the installation instructions for [using a single database](/docs/laravel-multitenancy/v4/installation/using-a-single-database).

If you want to use separate databases for each tenant, head over to the installation instructions for [using multiple databases](/docs/laravel-multitenancy/v3/installation/using-multiple-databases).
If you want to use separate databases for each tenant, head over to the installation instructions for [using multiple databases](/docs/laravel-multitenancy/v4/installation/using-multiple-databases).
2 changes: 1 addition & 1 deletion docs/installation/determining-current-tenant.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ To use that tenant finder, specify its class name in the `tenant_finder` key of
'tenant_finder' => Spatie\Multitenancy\TenantFinder\DomainTenantFinder::class,
```

If you want to determine the "current" tenant some other way, you can [create a custom tenant finder](/docs/laravel-multitenancy/v3/basic-usage/automatically-determining-the-current-tenant/).
If you want to determine the "current" tenant some other way, you can [create a custom tenant finder](/docs/laravel-multitenancy/v4/basic-usage/automatically-determining-the-current-tenant/).
6 changes: 3 additions & 3 deletions docs/installation/using-a-single-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Using a single database
weight: 2
---

Before using the following instructions, make sure you have performed [the base installation steps](/docs/laravel-multitenancy/v3/installation/base-installation) first.
Before using the following instructions, make sure you have performed [the base installation steps](/docs/laravel-multitenancy/v4/installation/base-installation) first.

Only use the instructions on this page if you want to use one database.

Expand All @@ -22,6 +22,6 @@ This will create the `tenants` table which holds configuration per tenant.

### Next steps

When using multiple tenants, you probably want to [isolate the cache](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/prefixing-cache/) or use your own separated filesystems per tenant, ... These things are performed by [task classes](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) that will be executed when making a tenant the current one.
When using multiple tenants, you probably want to [isolate the cache](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/prefixing-cache/) or use your own separated filesystems per tenant, ... These things are performed by [task classes](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/overview/) that will be executed when making a tenant the current one.

The package also has an option to [make the queue tenant aware](/docs/laravel-multitenancy/v3/basic-usage/making-queues-tenant-aware/).
The package also has an option to [make the queue tenant aware](/docs/laravel-multitenancy/v4/basic-usage/making-queues-tenant-aware/).
10 changes: 5 additions & 5 deletions docs/installation/using-multiple-databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Using multiple databases
weight: 3
---

Before using the following instructions, make sure you have performed [the base installation steps](/docs/laravel-multitenancy/v3/installation/base-installation) first.
Before using the following instructions, make sure you have performed [the base installation steps](/docs/laravel-multitenancy/v4/installation/base-installation) first.

Only use the instructions on this page if you want each of your tenants to have their own database.

Expand Down Expand Up @@ -79,19 +79,19 @@ You should add this task to the `switch_tenant_tasks` key.
],
```

The package also provides [other tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) that you could optionally add to `switch_tenant_tasks`. You can also [create a custom task](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/creating-your-own-task/).
The package also provides [other tasks](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/overview/) that you could optionally add to `switch_tenant_tasks`. You can also [create a custom task](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/creating-your-own-task/).

### Creating tenant databases

Now that automatic database switching for tenants is configured, you can migrate the tenant databases. Because there are so many ways to go about it, the package does not handle creating databases. You should take care of creating new tenant databases in your own application code. A nice place to trigger this could be [when a `Tenant` model gets created](/docs/laravel-multitenancy/v3/advanced-usage/using-a-custom-tenant-model/#performing-actions-when-a-tenant-gets-created).
Now that automatic database switching for tenants is configured, you can migrate the tenant databases. Because there are so many ways to go about it, the package does not handle creating databases. You should take care of creating new tenant databases in your own application code. A nice place to trigger this could be [when a `Tenant` model gets created](/docs/laravel-multitenancy/v4/advanced-usage/using-a-custom-tenant-model/#performing-actions-when-a-tenant-gets-created).

If you want to get a feel of how the package works, you could create a couple of rows in the `tenants` table, fill the `database` attribute and manually create those databases.

### Migrating tenant databases

When you want to migrate tenant databases, all future migrations should be stored in `database/migrations`.

To perform these migrations, you can use [the `tenants:migrate` command](/docs/laravel-multitenancy/v3/advanced-usage/executing-artisan-commands-for-each-tenant). This command will loop over all rows in the `tenants` table. It will make each tenant the current one, and migrate the database.
To perform these migrations, you can use [the `tenants:migrate` command](/docs/laravel-multitenancy/v4/advanced-usage/executing-artisan-commands-for-each-tenant). This command will loop over all rows in the `tenants` table. It will make each tenant the current one, and migrate the database.

```bash
php artisan tenants:artisan "migrate --database=tenant"
Expand Down Expand Up @@ -144,4 +144,4 @@ All models in your project should either use the `UsesLandlordConnection` or `Us

### Next steps

When using multiple tenants, you probably want to [isolate the cache](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/prefixing-cache/). This is performed by task classes that will be executed when making a tenant the current one.
When using multiple tenants, you probably want to [isolate the cache](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/prefixing-cache/). This is performed by task classes that will be executed when making a tenant the current one.
6 changes: 3 additions & 3 deletions docs/using-tasks-to-prepare-the-environment/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ When making a tenant the current one, the tasks inside the `switch_tenant_tasks`

The philosophy of this package is that it should only provide the bare essentials to enable multitenancy. That's why it only provides two tasks out of the box. These tasks serve as example implementations.

You can easily [create your own tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/creating-your-own-task/) that fit your particular project.
You can easily [create your own tasks](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/creating-your-own-task/) that fit your particular project.

The package ships with these tasks:

- [switch the tenant database](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/switching-databases) (required when using multiple tenant databases)
- [prefixing the cache](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/prefixing-cache)
- [switch the tenant database](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/switching-databases) (required when using multiple tenant databases)
- [prefixing the cache](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/prefixing-cache)

These tasks are optional. When you need one, just add it to the `switch_tenant_tasks` key of the `multitenancy` config file.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ cache('key') // returns 'value-for-another-tenant'

Behind the scenes, this works by dynamically changing the `cache.prefix` in the `cache` config file whenever another tenant is made current.

If you want to make the cache tenant aware in another way, you should [create your own task](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/creating-your-own-task/). You can take a look at the source code of `PrefixCacheTask` for inspiration.
If you want to make the cache tenant aware in another way, you should [create your own task](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/creating-your-own-task/). You can take a look at the source code of `PrefixCacheTask` for inspiration.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ To use this task, you should add it to the `switch_tenant_tasks` key in the `mul
],
```

If you want to change other database connection properties beside the database name, you should [create your own task](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/creating-your-own-task/). You can take a look at the source code of `SwitchTenantDatabaseTask` for inspiration.
If you want to change other database connection properties beside the database name, you should [create your own task](/docs/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/creating-your-own-task/). You can take a look at the source code of `SwitchTenantDatabaseTask` for inspiration.

0 comments on commit a9d125c

Please sign in to comment.