How to check a range of values in laravel?

1

Hi, I have a question: I want to know how I can check that my status is 1 or 2 within the laravel credentials.

protected function credentials(Request $request)
{
    return array_merge($request->only($this->username(), 'password'), ['state' => 1]);
    //return $request->only($this->username(), 'password');
}

This is the code I use, I want state to be greater than or equal to 1

    
asked by Carlos Alexander Mojica Guarda 14.12.2017 в 17:10
source

1 answer

0

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

    
answered by 18.12.2017 в 11:07