As Laravel is built, you can and can not.
Additional conditions may be added exactly as you can see in your code. Not one, but several, but ...
You can not set conditions other than '='
or repeat the field (column) with different values since the foreach
that converts the condition array to $query->where($key, $value);
is loaded / crushed the duplicate keys in array
of conditions. If as an alternative it happened to you, like me, to do something similar to ['state' => 1, 'state' => 2]
, it's no use, given that in a duplicate key you keep the second value.
An alternative is to modify the function retrieveByCredentials(array $credentials)
of the file vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php
where it says:
foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'password')) {
$query->where($key, $value);
}
}
must be left:
foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'password')) {
if(is_array($value)) {
$query->whereIn($key, $value);
} else {
$query->where($key, $value);
}
}
}
So that you can add conditions in array:
protected function credentials(Request $request)
{
return array_merge($request->only($this->username(), 'password'), ['state' => [1, 2, 3]]);
//return $request->only($this->username(), 'password');
}
To make this change you can follow the instructions in this Stack question: Laravel 5.4 EloquentUserProvider override validateCredentials