PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How to change Laravel Auth::attempt method query?

 April 19, 2022     laravel, laravel-authentication     No comments   

Issue

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?


Solution

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



Answered By - apokryfos
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing