PHPackages                             fourdotsix/tenancy-mapping - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fourdotsix/tenancy-mapping

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fourdotsix/tenancy-mapping
==========================

Tenant data handling for stancl/tenancy

1.0.0(6mo ago)115[2 PRs](https://github.com/fourdotsix/tenancy-mapping/pulls)MITPHPPHP ^8.3CI passing

Since Nov 8Pushed 4mo agoCompare

[ Source](https://github.com/fourdotsix/tenancy-mapping)[ Packagist](https://packagist.org/packages/fourdotsix/tenancy-mapping)[ Docs](https://github.com/fourdotsix/tenancy-mapping)[ Fund](https://www.buymeacoffee.com/shashwatmishra)[ GitHub Sponsors](https://github.com/secrethash)[ RSS](/packages/fourdotsix-tenancy-mapping/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (14)Versions (4)Used By (0)

[![fourdotsix.com Logo](https://camo.githubusercontent.com/c9eeee589413dbdfb148b715364d51bb5dbe3d4690ab7090defedf39cb10d761/68747470733a2f2f63646e2e666f7572646f747369782e636f6d2f696d616765732f342e362f6c6f676f2f342e362d62696d692e737667)](https://fourdotsix.com)
**four●six** // tenancy-mapping

Tenant data handling (mapping)
==============================

[](#tenant-data-handling-mapping)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b34a64f2f69d43168226f859727ea004307ada9247430e6951080c06267cc30f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f7572646f747369782f74656e616e63792d6d617070696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fourdotsix/tenancy-mapping)[![GitHub Tests Action Status](https://camo.githubusercontent.com/787a2300fd3c381924d2b955e9ed508bc6a5a930f53a7e09d0107594ea3fc6c8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f666f7572646f747369782f74656e616e63792d6d617070696e672f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/fourdotsix/tenancy-mapping/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/96b3b3d8c3eb215403cc00224f7cb6b0f37dbcb967b209d9644fbc8f95cbf05c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f666f7572646f747369782f74656e616e63792d6d617070696e672f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/fourdotsix/tenancy-mapping/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/e43b4a6cbcda730b5ce1ebbea2a97dba5c2f12387861a6370a5c38ccd5a949aa/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666f7572646f747369782f74656e616e63792d6d617070696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fourdotsix/tenancy-mapping)

Tenant specific data handling for [stancl/tenancy](https://github.com/archtechx/tenancy).

What?
-----

[](#what)

Mappings are the key-value pairs that can be used to get non-sensitive data while maintaining the tenant context. These are compiled and stored in an in-memory database, ex: Redis, giving faster data access. On a base level, there are two types of mappings:

- Descriptors: These are useful when different tenant types refers to some part of the application differently.

    For example: An LMS SaaS application, can serve both businesses and educational institutions. An educational institution might refer to it's learners as Students while businesses might refer them as Employees. In such cases descriptor comes handy for dynamic descriptors. The descriptor mappings would be as such:

    ```
    ---
    # file: university.yml
    name: Institution ORG mappings
    description: Mappings for tenant university.example.co
    mappings:
        learner: student
        institute: university
    ```

    This can be accessed as such:

    ```
        descriptor('learner')->title(); // Student
        descriptor(key: 'institute', default: 'institute'); // university
        descriptor(key: 'institute', default: 'institute', i18n: true); // महाविद्यालय
    ```
- Settings: As the name suggests, these can be used for non-sensitive tenant specific settings. It provides faster access to settings data using key-value pairs and keeping these in YAML files can provide an audit trail for changes in GIT. Settings are merged by default, i.e., there can be a separate tenant's YAML file created with overrides and compilation will merge the settings in generic settings YAML file during compilation. This can be disabled in config file, if needed.

Why?
----

[](#why)

When creating a SaaS application, the biggest challenge is customization according to the tenant. Not all organizations are the same, therefore, tailoring specific software features can become impossible while onboarding new tenants and scalability can really take a hit.

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

[](#installation)

1. Install the package via composer:

    ```
    composer require fourdotsix/tenancy-mapping
    ```
2. Publish the config file with:

    ```
    php artisan vendor:publish --tag="tenancy-mapping-config"
    ```
3. Publish the migrations with:

    ```
    php artisan vendor:publish --tag="tenancy-mapping-migrations"
    ```
4. Publish the mappings folder with:

    ```
    php artisan vendor:publish --tag="tenancy-mapping-mappings"
    ```
5. Add the Tenancy Bootstrapper in your `config/tenancy.php`:

    ```
    'bootstrappers' => [
        ...
        // Integration bootstrappers
        ...
        Fourdotsix\TenancyMapping\Bootstrappers\MappingTenancyBootstrapper::class,
    ],
    ```
6. (optional|**recommended)** Set `TenantType` enum: This helps in creating a consistent and safe way of defining tenant types. Although it's optional, it's still recommended.

    - Create an Enum and implement `Fourdotsix\TenancyMapping\Contracts\TenantType`, Ex:

        ```
