ahmeda's avatar

How to get to tenants data throw central admin panel ?

The question to anyone used this package: https://tenancyforlaravel.com/

My idea: I wanna to create an e-commerce app contains multi-tenants with multi-database, that's mean I have an admin panel for the main admin and another admin panel for tenants (vendor)

I installed the PK above the all things are fine!

My question: How Can The Admin panel(main one) reach to the tenants data? like I need count the total products for all tenants and take some statistics for tenants!

0 likes
8 replies
prospero's avatar

Tenancy have some methods for that, to switch or set the connection to handle tenant's db. For example, Tenancy::getTenant(), setTenant($tenant),identifyTenant()...

For example, if you want retrieve the products data from a specific tenant, is something like:

public function getTenantProducts($tenant)
{
    Tenancy::setTenant($tenant);
     $product = App\Models\Tenant\Product::all();
}

Hope this help you a little

1 like
deansatch's avatar

tenancy()->initialize($tenant); then run your tenant code

$tenant is the desired tenant model eg Tenant::find(‘my tenant’)

When you are done tenancy()->end() to switch back to central

1 like
ahmeda's avatar

Is there a reference in Docs for initialize ?

ckalita's avatar

Yes, of course: Manual Initialization

However, the code above will place you in the tenant context without leaving it.

How about this instead:

    public function getTenantProducts($tenant)
    {
        return $tenant->run(fn() => \App\Models\Tenant\Product::all());
    }

this way you will switch into tenant for a single query and will return to central context seamlessly

1 like
martinbean's avatar

@jean_ali Why split data across separate databases if you actually want to view it all in a single context (your admin panel)?

I really don’t understand this fetish of multi-database multitenancy. Foreign keys in a database work just fine and have done for decades.

1 like
jlrdw's avatar

@jean_ali before making any final decisions why don't you watch the video right here on laracasts that covers multi tenancy.

1 like
cletous's avatar

@martinbean separating a multi-tenant application into multi-database helps with perfomance. Sometimes a single tenant might need more server resources than the rest of tenants so if everyone is under 1 bucket then everyone's perfomance is jeopardized because of 1 demanding tenant. Separating allows you to even allocate server resources as needed to each tenant and even bill them differently according to usage

1 like

Please or to participate in this conversation.