Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Allow array/collections in Auth::attempt method #24620

Merged
merged 5 commits into from
Jun 20, 2018

Conversation

VinceG
Copy link
Contributor

@VinceG VinceG commented Jun 17, 2018

Currently Auth::attempt allows to pass in additional conditions, however, it won't allow passing in arrays as conditions.

Current behavior

Auth::attempt([
                'email' => $request->get('email'),
                'password' => $request->get('password'),
                'user_type' =>[1, 2, 3],
                'active' => 'Y'
          ], $request->get('remember'))

Results in the following query:

select * from `user` where `email` = 'xxx' and `user_type` = '1' and `active` = 'Y' limit 1

With this PR the above code will generate the following query instead:

select * from `user` where `email` = 'xxx' and `user_type` in ('1', '2', '3') and `active` = 'Y' limit 1

This supports both arrays and collections so the following is exactly the same as the example above

Auth::attempt([
                'email' => $request->get('email'),
                'password' => $request->get('password'),
                'user_type' => collect([1, 2, 3]),
                'active' => 'Y'
          ], $request->get('remember'))

I hope i added the test correctly. All tests seem to pass with this change.

Based on the Contributing guide i added this to the current release since it's a minor change and has no backwards compatibility breaks.

Thank You.

@GrahamCampbell GrahamCampbell changed the title Allow array/collections in Auth::attempt method [5.6] Allow array/collections in Auth::attempt method Jun 18, 2018
@VinceG
Copy link
Contributor Author

VinceG commented Jun 18, 2018

@lucasmichot @dwightwatson Thank You for the input.

It seems that Collection does implement Arrayable however the following code does not seem to work as it should.

Auth::attempt([
                'email' => $request->get('email'),
                'password' => $request->get('password'),
                'user_type' => collect([1, 2, 3]),
                'active' => 'Y'
          ], $request->get('remember'))
            if ( is_array($value) || $value instanceof Arrayable ) {
                $query->whereIn($key, $value);
            } else {
                $query->where($key, $value);
            }
select * from `user` where `email` = 'xxx' and `user_type` = '1' and `active` = '2' limit 1

in the code above if i do

dd($value instanceof Arrayable); it returns false

Any idea why?

@lucasmichot
Copy link
Contributor

lucasmichot commented Jun 18, 2018

See #24620 (comment) 😄
use Illuminate\Contracts\Support\Arrayable; is missing

@VinceG
Copy link
Contributor Author

VinceG commented Jun 18, 2018

@lucasmichot @dwightwatson Thanks, That did it. I've updated the PR.

@lucasmichot
Copy link
Contributor

So I believe this should work now

@taylorotwell taylorotwell merged commit abf1e2c into laravel:5.6 Jun 20, 2018
@decadence
Copy link
Contributor

Would be much better if where transforms array in value to whereIn call automatically in Builder.
#19126
#20470

@VinceG VinceG deleted the allow-array-in-auth-attempt branch September 3, 2020 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants