Home > OS >  How to change Laravel Auth::attempt method query?
How to change Laravel Auth::attempt method query?

Time:04-06

I need to change the default Laravel query from Auth::attempt().

In the docs I saw two ways:

  • Pass more parameters to the Auth::attempt() method;
  • Create my own User provider.

The first solution not works for me, because I need to add a OR clause on the query (SELECT * FROM users WHERE email=? OR phone=?), and not an AND clause. The second solution should be the correct one, but very complex, as I only need the retrieveByCredentials method.

Is there another way to make this?

CodePudding user response:

Not sure if it's documented but you can do the following:

$givenField = '123456';

Auth::attempt([
  'email' => function ($query) use ($givenField) { 
     $query->where('email', $givenField)
       ->orWhere('phone', $givenField)->from('users')->select('email');
]);

This is a bit hacky. It usually allows you to query other tables but here it's the same one

  • Related