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

Sajjad Ali's avatar

UserFactory Hashed password not working

I'm following this tutorial. https://laracasts.com/series/multitenancy-in-practice/episodes/2

Created hashed password in UserFactory.

return [
    'name' => fake()->name(),
    'email' => fake()->safeEmail(),
    'email_verified_at' => now(),
    'role' => "admin",
    'password' => Hash::make('password'), // password
    'remember_token' => Str::random(10),
    'tenant_id' => \App\Models\Tenant::factory()
];

When I run User factory for the first time for 10 users in tinker and set ['tenant_id' = 1], the "password" work fine for all 10 users. But when I don't add tenant_id, e.g: App\Models\User::factory(10)->create() the users don't work and display These credentials do not match our records. error.

Any idea what am I doing wrong?

Please note that, by following that tutorial, I created a listener which adds tenant_id to the session when the user login, and then a global scope returns records only for that tenant_id. Not sure if this issue could be related to that.

0 likes
5 replies
MohamedTammam's avatar

The problem with the tenant_id not with the password

How are you logging in? and can you please share your listener code.

Sajjad Ali's avatar

@MohamedTammam Thanks for the reply.

This is the code in the SetTenantIdInSession listener.

public function handle($event)
{
    session()->put('tenant_id', $event->user->tenant_id);
}

This is inside EventServiceProvider.php to get $event data when the user login.

Login::class => [
    SetTenantIdInSession::class,
]

And this is inside TenantScope.php

public function apply(Builder $builder, Model $model)
{
    if (session()->has('tenant_id')) {
        $builder->where('tenant_id', '=', session()->get('tenant_id'));
    }
}
MohamedTammam's avatar

@Sajjad Ali The line of code will return null if you don't add tenant_id to the user record in the database

session()->put('tenant_id', $event->user->tenant_id);
Sajjad Ali's avatar

@MohamedTammam tenant_id is being added to each user record as you can see in the factory code I posted above. 'tenant_id' => \App\Models\Tenant::factory()

And even if this returns null then it will not show any record related to that tenant or maybe throw an error. Why I'm having login issue? That's strange.

Sajjad Ali's avatar
Sajjad Ali
OP
Best Answer
Level 7

The problem is with the Session listener or retrieving tenant_id from the session. Not sure.

The problem is solved by getting tenant_id from Auth class Auth::user()->tenant_id instead of session.

Please or to participate in this conversation.