mirror of
https://github.com/stancl/tenancy-docs.git
synced 2025-12-12 10:14:03 +00:00
123 lines
2.7 KiB
Markdown
123 lines
2.7 KiB
Markdown
---
|
|
title: Tenant Manager
|
|
description: Tenant Manager
|
|
extends: _layouts.documentation
|
|
section: content
|
|
---
|
|
|
|
# Tenant Manager {#tenant-manager}
|
|
|
|
This page documents a couple of `TenantManager` methods you may find useful.
|
|
|
|
To call methods on `TenantManager`, you may use the `tenancy()` helper or the `Tenancy` facade.
|
|
|
|
### Find tenant by id
|
|
|
|
```php
|
|
>>> \Tenancy::find('b07aa3b0-dc68-11e9-9352-9159b2055c42')
|
|
=> Stancl\Tenancy\Tenant {#3099
|
|
+data: [
|
|
"id" => "b07aa3b0-dc68-11e9-9352-9159b2055c42",
|
|
"plan" => "free",
|
|
],
|
|
+domains: [
|
|
"foo.localhost",
|
|
],
|
|
}
|
|
```
|
|
|
|
### Find tenant by domain
|
|
|
|
|
|
```php
|
|
>>> tenancy()->findByDomain('bar.localhost')
|
|
=> Stancl\Tenancy\Tenant {#3091
|
|
+data: [
|
|
"id" => "b38b2bd0-dc68-11e9-adfc-ede94ab3b264",
|
|
],
|
|
+domains: [
|
|
"bar.localhost",
|
|
],
|
|
}
|
|
```
|
|
|
|
### Getting the current tenant
|
|
|
|
One more way to get the current [tenant]({{ $page->link('tenants') }}) is to call `getTenant()` on `TenantManager`:
|
|
|
|
```php
|
|
tenancy()->getTenant()
|
|
```
|
|
|
|
If you want to get the value of a specific key from the array, you can an argument with the key.
|
|
|
|
```php
|
|
tenancy()->getTenant('id') // Does the same thing as tenant('id')
|
|
```
|
|
|
|
### Getting all tenants
|
|
|
|
This method returns a collection of arrays.
|
|
|
|
```php
|
|
>>> tenancy()->all()
|
|
=> Illuminate\Support\Collection {#3080
|
|
all: [
|
|
Stancl\Tenancy\Tenant {#3076
|
|
+data: [
|
|
"id" => "b07aa3b0-dc68-11e9-9352-9159b2055c42",
|
|
],
|
|
+domains: [
|
|
"foo.localhost",
|
|
],
|
|
},
|
|
Stancl\Tenancy\Tenant {#3075
|
|
+data: [
|
|
"id" => "b38b2bd0-dc68-11e9-adfc-ede94ab3b264",
|
|
],
|
|
+domains: [
|
|
"bar.localhost",
|
|
],
|
|
},
|
|
],
|
|
}
|
|
>>> tenancy()->all()->pluck('domains')
|
|
=> Illuminate\Support\Collection {#3108
|
|
all: [
|
|
[
|
|
"foo.localhost",
|
|
],
|
|
[
|
|
"bar.localhost",
|
|
],
|
|
],
|
|
}
|
|
```
|
|
|
|
### Deleting a tenant
|
|
|
|
```php
|
|
>>> $tenant = tenancy()->findByDomain('foo.localhost');
|
|
=> Stancl\Tenancy\Tenant {#3119
|
|
+data: [
|
|
"id" => "b07aa3b0-dc68-11e9-9352-9159b2055c42",
|
|
"plan" => "free",
|
|
],
|
|
+domains: [
|
|
"foo.localhost",
|
|
],
|
|
}
|
|
>>> $tenant->delete();
|
|
=> true
|
|
```
|
|
|
|
NOTE: This doesn't delete the tenant's database.
|
|
|
|
If you want to delete it, get the database name prior to deleting the tenant using `getDatabaseName()`.
|
|
|
|
```php
|
|
>>> $tenant->getDatabaseName()
|
|
=> "tenant67412a60-1c01-11e9-a9e9-f799baa56fd9"
|
|
```
|
|
|
|
If you want tenant databases to be deleted automatically, you may use the [`delete_database_after_tenant_deletion` configuration]({{ $page->link('configuration#delete-database-after-tenant-deletion') }})
|