mirror of
https://github.com/archtechx/tenancy.git
synced 2026-11-04 13:40:44 +00:00
Automatic multi-tenancy for Laravel. No code changes needed.
https://tenancyforlaravel.com
- PHP 99.2%
- Nushell 0.3%
- Dockerfile 0.3%
> Minor breaking change: if you have custom `tenancy_db_*` keys on your
tenants, you will need to override your Tenant model's `database()` to
return `parent::database()->allow(['your', 'extra', 'keys'])`,
potentially with custom validation (`->validate(['your', 'keys'],
bool|Closure)` where the second argument specifies `true` for default
validation logic, `false` for **no validation**, or a custom Closure
returning a boolean or throwing an exception).
`HasDatabase::database()` passed every `tenancy_db_*` attribute into the
tenant connection config. Since the `Tenant` model is unguarded, an app
that mass-assigns request data into a tenant let the requester set keys
like `url`, `read` or `write`, which repoint the connection, or
`isolation_level`, `charset` and `collation`, which `MySqlConnector`
interpolates into SQL on connect.
`DatabaseConfig` now only accepts `host`, `port`, `charset`,
`collation`, `username` and `password`. Any other key throws an
exception naming the key and the tenant. Values are validated with
`validateParameter()`, except usernames and passwords, which the PDO
drivers pass to the server separately or escape before use. `host` uses
the default character list plus `.:` and `port` only allows digits.
Other keys can be allowed by overriding `database()` in the Tenant
model:
```php
// Tenant
public function database(): DatabaseConfig
{
return parent::database()
->allow(['sslmode', 'options']) // '*' allows any key
->validate('options', false); // true = default rules, false = none, or a closure
}
```
`allow()` accepts `string|string[]`, `validate()` accepts
`(string|string[], bool|Closure)` where the first argument is either one
or multiple key names and the second argument is the validator — true
for the default rules, false for **no validation**, or a Closure for
custom rules.
`allow('*')` makes all keys allowed. `validate('*', $validator)`
specifies the validator used for keys that don't have *their own*
validator specified.
Allowed keys use the default validation unless `validate()` says
otherwise.
Also fixes `$harden` ignoring `tenancy_db_name` when it's a custom
column.
See the _Customizing databases_ page in v4 docs.
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
||
|---|---|---|
| .github | ||
| art | ||
| assets | ||
| doctum | ||
| extensions | ||
| src | ||
| tests | ||
| typedefs | ||
| .gitattributes | ||
| .gitignore | ||
| .nvim.lua | ||
| .php-cs-fixer.php | ||
| AGENTS.md | ||
| CLAUDE.md | ||
| composer.json | ||
| CONTRIBUTING.md | ||
| docker-compose-m1.override.yml | ||
| docker-compose.yml | ||
| Dockerfile | ||
| DONATIONS.md | ||
| INTERNAL.md | ||
| LICENSE | ||
| phpstan.neon | ||
| phpunit.xml | ||
| README.md | ||
| static_properties.nu | ||
| SUPPORT.md | ||
| t | ||
| test | ||
Tenancy for Laravel — stancl/tenancy
Automatic multi-tenancy for your Laravel app.
You won't have to change a thing in your application's code.
- ✔️ No model traits to change database connection
- ✔️ No replacing of Laravel classes (
Cache,Storage, ...) with tenancy-aware classes - ✔️ Built-in tenant identification based on hostname (including second level domains)
Documentation
Documentation can be found here: https://v4.tenancyforlaravel.com
Need help?
Credits
- Package created by Samuel Štancl
- Design help & current logo by Florian Karsten
