Skip to content

Module Manager Helpers

The Module Manager provides a set of global helper functions that simplify working with multi-tenancy, modules, configuration settings, and authorization throughout your application.

1. Multi-Tenancy Helpers

These helpers provide easy access to multi-tenancy configuration and state:

isMultiTenant(): bool

Checks if multi-tenancy is enabled in the application.

php
// Check if we need to handle tenant-specific logic
if (isMultiTenant()) {
    // Multi-tenancy is enabled, apply tenant-specific logic
}

multiDatabase(): bool

Checks if multi-database mode is enabled, which determines if each tenant has its own database.

php
if (multiDatabase()) {
    // Each tenant has its own database
}

tenantClass(): string

Gets the fully qualified class name of the tenant model configured in the application.

php
$tenantClass = tenantClass(); // e.g. 'App\Models\Company'
$tenant = new $tenantClass();

tenantIdColumn(): string

Gets the column name used for tenant IDs in the database (default is usually 'tenant_id').

php
$column = tenantIdColumn(); // e.g. 'tenant_id'
$query->where($column, $tenantId);

tenant(): ?Model

Gets the current authenticated tenant from Filament.

php
$currentTenant = tenant();
if ($currentTenant) {
    echo "Current tenant: " . $currentTenant->name;
}

tenantUserTable(): string

Gets the name of the pivot table that connects tenants and users.

php
$pivotTable = tenantUserTable(); // e.g. 'tenant_users'

tenantRelationshipName(): string

Gets the camelCase relationship name for the tenant model, derived from the class name.

php
$relationshipName = tenantRelationshipName(); // e.g. 'company'
$model->$relationshipName; // Access the tenant relationship

2. User Helpers

These helpers simplify working with users:

userClass(): string

Gets the fully qualified class name of the user model configured in the application.

php
$userClass = userClass(); // e.g. 'App\Models\User'
$user = $userClass::find(1);

user(): ?User

Gets the current authenticated user.

php
$currentUser = user();
if ($currentUser) {
    echo "Hello, " . $currentUser->name;
}

userIdColumn(): string

Gets the user ID column name used in relationship tables.

php
$column = userIdColumn(); // e.g. 'user_id'
$query->where($column, $userId);

3. Module Utilities

These helpers assist with module identification and configuration:

getModuleNameFromClass(string|object $class): ?string

Extracts the module name from a class name or object.

php
$module = getModuleNameFromClass('Modules\Blog\Models\Post'); // Returns 'Blog'
$module = getModuleNameFromClass($postObject); // Returns 'Blog'

fromModuleManager(string $key, mixed $default): mixed

Gets module-specific configuration from the module-manager config file.

php
$blogConfig = fromModuleManager('blog.settings', ['posts_per_page' => 10]);

getModuleManagerConfig(string $key, mixed $default): mixed

Gets global module-manager configuration values.

php
$checkPermissions = getModuleManagerConfig('check_permissions', true);

4. Authorization Helpers

These helpers simplify authorization checks:

authorizeAction(string|array|AuthorizeAction $action, Model|string|null $model): bool

Authorizes an action or an array of actions for the authenticated user on a specific model.

php
// Single action check
if (authorizeAction('view', $post)) {
    // User can view the post
}

// Multiple actions with enum
if (authorizeAction([AuthorizeAction::Create, AuthorizeAction::Update], Post::class)) {
    // User can both create and update posts
}

checkPermissions(): bool

Checks if the permission verification system is enabled.

php
if (checkPermissions()) {
    // Permission checking is enabled
}

userInteractsWithModules(): string

Gets the fully qualified class name of the trait that handles user interactions with modules.

php
$traitClass = userInteractsWithModules(); // e.g. 'App\Traits\UserInteractsWithModules'
checkIfTraitIsUsed($traitClass, userClass());

tenantInteractsWithModules(): string

Gets the fully qualified class name of the trait that handles tenant interactions with modules.

php
$traitClass = tenantInteractsWithModules(); // e.g. 'App\Traits\TenantInteractsWithModules'
checkIfTraitIsUsed($traitClass, tenantClass());

5. Utility Helpers

Additional utility functions to help with common operations:

mergeTenantId(array $arrayBase, ?int $fallbackId = null): array

Automatically adds the current tenant ID to an array of attributes. Useful for creating new models.

php
$postData = [
    'title' => 'New Post',
    'content' => 'Post content'
];

// Will add tenant_id if multi-tenancy is enabled
$postData = mergeTenantId($postData, fallBackId: Company::find(1)->getKey());

Post::create($postData);

checkIfTraitIsUsed(string $trait, string $targetClass): void

Verifies if a specific trait is used in a class. Throws an exception if the trait is not used.

php
// Will throw an exception if UserInteractsWithModules trait is not used in the User class
checkIfTraitIsUsed('App\Traits\UserInteractsWithModules', 'App\Models\User');

Usage in Multi-Tenant Applications

In a multi-tenant application, these helpers work together to provide a seamless experience:

php
// Check if multi-tenancy is enabled
if (isMultiTenant()) {
    // Get the current tenant
    $currentTenant = tenant();

    // Query only records belonging to the current tenant
    $posts = Post::query()
        ->where(tenantIdColumn(), $currentTenant->getKey())
        ->get();

    // Or simply rely on the HasTenant trait to automatically scope queries
    $posts = Post::all(); // Already scoped to current tenant if Post uses HasTenant

    // When creating records, automatically include tenant ID
    $post = Post::create(mergeTenantId([
        'title' => 'New Post',
        'content' => 'Content...'
    ]));

    // Check permissions
    if (authorizeAction('create', Post::class)) {
        // User can create posts for the current tenant
    }
}

These helpers form a cohesive system that makes multi-tenant application development more efficient and less error-prone.