I am using Laravel 8, and I need to validate a user using data from 2 tables: users and customers. When user logs in, they will input 3 values: email, password and account. "Account" field comes from customer table.
So I need to do a join to access "account" field from "customers" table.
One option I see is:
public function validateCredentials(UserContract $user, array $credentials) {
$plain = $credentials['password'];
//Custom Query
return $this->hasher->check($plain, $user->getAuthPassword());
}
In section "Custom Query", I can do a query to get customer data using $user->customer_id and check if matches $credentials['account'], but not sure if it is the best way.
Thanks in advance for your help.
I end up building a Custom Provider, and override "retrieveByCredentials" method, that made the trick.
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials) ||
(count($credentials) === 1 &&
Str::contains($this->firstCredentialKey($credentials), 'password'))) {
return;
}
$query = User::select(['customer.identifier', 'users.*'])->join('customer', 'customer_id', '=', 'customer.id');
foreach ($credentials as $key => $value) {
if (Str::contains($key, 'password')) {
continue;
}
if($key == 'account') {
$query->where('customer.identifier', '=', $credentials['account']);
} else {
$query->where($key, $value);
}
}
return $query->first();
}