Database Tenant Provider

Sometimes you don't want to use Eloquent, or you want to access the database without the overhead of Eloquent models. In that case, you can use the database tenant provider.

Introduction

The database tenant provider is a driver for Sprouts tenant provider functionality that uses the database and a POPO (plain old PHP object) as your tenant.

Using

When configuring your provider in the multitenancy config you only need to do two things to use this tenant provider. Set the driver to database and provide a table name using the table config option. An example config entry for this tenant provider looks like so:

1'providers' => [
2 'tenants' => [
3 'driver' => 'database',
4 'table' => 'tenants'
5 ],
6]

This tenant provider was created, primarily, to act as a secondary provider for cases where Eloquent would add unnecessary overhead. The idea was that the eloquent tenant provider would still be used in most cases, but this would be used in short-lived background operations. So, if you're also using it this way, you can provide an Eloquent model class instead of a table, and the models table name and connection name will be used.

1'providers' => [
2 'tenants' => [
3 'driver' => 'database',
4 'table' => MyTenantModel::class
5 ],
6]

By default, this tenant provider will use the default database connection, but if you want it to use another, you can provide it with the connection option.

1'providers' => [
2 'tenants' => [
3 'driver' => 'database',
4 'table' => 'tenants',
5 'connection' => 'custom-connection'
6 ],
7]

This tenant provider makes use of the Sprout\Support\GenericTenant class as its implementation of the Tenant contract, but if you want to provide your own, you can provide a class name using the entity option.

1'providers' => [
2 'tenants' => [
3 'driver' => 'database',
4 'table' => 'tenants',
5 'entity' => MyTenant::class,
6 ],
7]

The entity you provide should have a constructor that accepts an array of attributes, stored as key-value pairs.

When the database tenant provider is created, the entity class will be validated and must meet the following criteria.

  • It MUST exist at runtime.
  • It MUST implement the Sprout\Contracts\Tenant interface, either directly or indirectly.

If the provided value does not meet these criteria, a misconfiguration exception will be thrown.