orchestral / tenanti

[Package] Multi-tenant Database Schema Manager for Laravel
MIT License
587 stars 53 forks source link

Queries with() are failing. #48

Closed justinatack closed 7 years ago

justinatack commented 7 years ago

Hello

I'm testing out Tenanti and I have successfully installed it, each time I create a new Tenant (Company) it runs the migrations just fine. The issue is I get an error when trying to load in related data using the with key.

I'm using a single database with tenant prefixed tables The tables in my database are as follows ...

company_1_members
company_1_migrations
companies
migrations

Here is the route and query ...

public function show($id)
{
    return Company::with(['members'])->find($id);
}

The error I get is ...

SQLSTATE[42S02]: Base table or view not found: 
1146 Table 'laravel_tenanti.members' doesn't exist 
(SQL: select * from `members` where `members`.`company_id` in (1))

I can see where the error is. The table name is incorrect, it should be ...

laravel_tenanti.company_1_members

I think I'm missing something ...

What should be in the middleware or attached to the query to make it scope correctly?

crynobone commented 7 years ago

This package only handle "Database/Schema Migration", you need to handle any Eloquent custom queries on your own.

justinatack commented 7 years ago

ThIs issue is not really related to a "custom" eloquent query. I don't think you read the issue properly.

The basic issue is "How do I get the correct users table". How does Tenanti switch to use the correct tenant tables according to the logged in user?

crynobone commented 7 years ago

When you go with multi-tenant on single database, it does requires "custom" eloquent query, there no going around it with what Laravel is offering at the moment.