Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maison012's avatar

Multi tenant is not creating user foreach tenant

I am trying to migrate my laravel app into multitenant app, and i have following this documentation https://tenancyforlaravel.com/docs/v3/quickstart/ I have followed each step until Creating tenants part i also have created 2 different tenants

$ php artisan tinker
>>> $tenant1 = App\Models\Tenant::create(['id' => 'foo']);
>>> $tenant1->domains()->create(['domain' => 'foo.localhost']);
>>>
>>> $tenant2 = App\Models\Tenant::create(['id' => 'bar']);
>>> $tenant2->domains()->create(['domain' => 'bar.localhost']);

But i am stuck here

App\Models\Tenant::all()->runForEach(function () {
    App\Models\User::factory()->create();
});

I run this on tinker but is not creating me any record on database select * from users return me as empty. Also have moved migrations all on migrations\tenant folder except tenant_table & domain_table witch is created from tenancy install.

If i do User::all() on tinker after i run function to create user foreach tenant is how me users, but on mysql table is not showing any record. Any idea how to fix this?

0 likes
10 replies
LaryAI's avatar
Level 58

It's possible that the issue is related to the database connection. When using multi-tenancy, each tenant should have its own database connection. Make sure that you have set up the database connections correctly in your config/database.php file.

Also, make sure that you are running the migrations for each tenant separately. You can do this by running the following command:

php artisan tenancy:migrate --tenants=foo,bar

This will run the migrations for the foo and bar tenants.

If you have already done this and are still experiencing issues, try running the create method instead of the factory method to create the users. For example:

App\Models\Tenant::all()->runForEach(function () {
    App\Models\User::create([
        'name' => 'John Doe',
        'email' => '[email protected]',
        'password' => bcrypt('password'),
    ]);
});

This will create a user with the specified name, email, and password for each tenant.

If none of these solutions work, try debugging the issue by adding some dd statements to your code to see where it's failing. For example:

App\Models\Tenant::all()->runForEach(function () {
    dd('Creating user for tenant');
    App\Models\User::factory()->create();
});

This will output the string "Creating user for tenant" for each tenant, allowing you to see where the code is failing.

Snapey's avatar

sounds like when you run App\Models\Tenant:all() the result is being scoped in some way

running this in tinker, does it return the expected tenants?

1 like
Maison012's avatar

@Snapey I just saw on 2 other database are creaded tenanttest2 | | tenanttest3 but when i run on browser http://test3.localhost:8000/ i get Illuminate\Database\Eloquent\Collection {#1366 ▼ // routes\tenant.php:32 #items: [] #escapeWhenCastingToString: false } empty. Does this mean should i make some configuration on the database.php or any other configuration for application to know the new database when created?

Maison012's avatar

@Snapey In the documentation i does not see any part of creating another database connection. So is there any other documentation or any help how should i use this connection?

Snapey's avatar

@Maison012 The package should use it for you..

The tenants should be in the main DB and the tenant data in the per-tenant databases. So when you do select * from users in your mysql tooling, you need to be connected to the right database.

Did you do the Migrations section of the setup guide?

1 like
Maison012's avatar

@Snapey You mean this section from the tenancyforlaravel doc?

To have users in tenant databases, let's move the users table migration (the file database/migrations/2014_10_12_000000_create_users_table.php or similar) to database/migrations/tenant. This will prevent the table from being created in the central database, and it will be instead created in the tenant database when a tenant is created — thanks to our event setup.

Yes i do, i have moved all migraion on tenant folder except 2019_09_15_000020_create_domains_table, 2019_09_15_000010_create_tenants_table this two migrations

Snapey's avatar

@Maison012 but because it is an existing project, you already have a users table?

1 like
Maison012's avatar

@Snapey

Yes, this is my db i used from the begining of project test_crm and have this structure

+------------------------+
| domains                |
| failed_jobs            |
| migrations             |
| model_has_permissions  |
| model_has_roles        |
| password_resets        |
| permissions            |
| personal_access_tokens |
| products               |
| role_has_permissions   |
| roles                  |
| tenants                |
| users                  |
+------------------------+

it is basic

also this is tenanttest2 db is created when i create tinker i think with this structure

+------------------------+
| failed_jobs            |
| migrations             |
| model_has_permissions  |
| model_has_roles        |
| password_resets        |
| permissions            |
| personal_access_tokens |
| products               |
| role_has_permissions   |
| roles                  |
| users                  |
+------------------------+
Maison012's avatar

@Snapey

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test_crm.users' doesn't exist

I just run php artisan migrate:fresh but when i visit test2.localhost i see this messages couse now users table does not exist on test_crm DB. I have moved on migrations\tenant

On tenant route i also have this as is on documentation

Route::get('/', function () {

    dd(\App\Models\User::all());

    return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');

});
Maison012's avatar

[SOLUTION]

I found the problem, The mistake was mine as I had followed the installation steps of the `laravelfortenancy' package incorrectly, after following them step by step I think now it works properly.

But still thank you for trying to help me

Please or to participate in this conversation.