Logging in with multiple types of info I'm trying to let users log in with multiple columns (e.g. ID, email, phone). If one fails, it checks the next column.

I would like to minimise my if and else statements. Is there better way of doing it?

$email = $request-&gt;only('email', 'password');
        $ssn='';$badgenumber='';$ophone='';$fphone='';$pager='';


        if(preg_match("/^[0-9]+$/",$email['email']) == 1 &amp;&amp; strlen($email['email']) &lt; 6){
            $ssn = $request-&gt;only('ssn', 'password');
            $badgenumber = $request-&gt;only('badgenumber', 'password');
        }

        if(preg_match("/^[0-9]+$/",$email['email']) == 1 &amp;&amp; strlen($email['email']) &gt; 6){
            $ophone = $request-&gt;only('ophone', 'password');
            $fphone = $request-&gt;only('fphone', 'password');
            $pager = $request-&gt;only('pager', 'password');
        }

        if(filter_var($email['email'],FILTER_VALIDATE_EMAIL) === false)
            $email ='';

        $error = 1;

        try {
            // verify the $email and create a token for the user

            if ($error == 1 &amp;&amp; $email) {
                $error = 0;
                if (!$token = JWTAuth::attempt($email)) {
                    $error = 1;
                }
            }
            if ($error == 1 &amp;&amp; $ssn) {
                $error = 0;
                if (!$token = JWTAuth::attempt($ssn)) {
                    $error = 1;
                }
            }
            if ($error == 1 &amp;&amp; $badgenumber) {
                $error = 0;
                if (!$token = JWTAuth::attempt($badgenumber)) {
                    $error = 1;
                }
            }
            if ($error == 1 &amp;&amp; $ophone) {
                $error = 0;
                if (!$token = JWTAuth::attempt($ophone)) {
                    $error = 1;
                }
            }
            if ($error == 1 &amp;&amp; $fphone) {
                $error = 0;
                if (!$token = JWTAuth::attempt($fphone)) {
                    $error = 1;
                }
            }
            if ($error == 1 &amp;&amp; $pager) {
                $error = 0;
                if (!$token = JWTAuth::attempt($pager)) {
                    $error = 1;
                }
            }

            if($error == 1){
                return response()-&gt;json(['error' =&gt; 'invalid_credentials'], 401);
            }

        } catch (JWTException $e) {
            // something went wrong
            return response()-&gt;json(['error' =&gt; 'could_not_create_token'], 500);
        }
Since this is the backend to a login request, I assume that you expect just 2 inputs, the password and one of the others.  So why not use the following:

//gets an array of all inputs: email &amp; pass or ssn &amp; pass or phone &amp; pass...    
$credentials = $request-&gt;all(); 

try{
    if(!$token = JWTAuth::attempt($credentials)){
        return response()-&gt;json(['error' =&gt; 'invalid_credentials'], 401);
    }

} catch (JWTException $e){
    return response()-&gt;json(['error' =&gt; 'could_not_create_token'], 500);
}



  i would like to minimise my if and else what would you guys recommend?


Hint: If you find yourself doing something over and over and over, you probably need a loop. Since this is a login form and that you expect just 1 variable besides the password, don't have so many vars in your script: $email, $ssn, $pager, badgenumber, ophone, fphone, ssn... just use $identifier.  If you really need to cycle through all possibilities build an array and loop until you find the one the user submitted:

$possibilities = ['email','ssn','badgenumber','ophone'...];
$identifier = '';
foreach ($possibilities as $i){
    if($request-&gt;has($i)){
        $identifier = $request-&gt;input($i);
        break; //exit the foreach loop b/c we've found the identifier
    }
}
$credentials = [$identifier, $request-&gt;input('password')];


Then you can attempt to create the token with those credentials.  Beware I've never used Laravel, but I wrote my answer after going over this helpful page on how to access requests.

Quick sidenote: I notice that you have a check that can be true only if email has length &lt; 6, and another that can be true only if email has length > 6.  What if email has length===6 ?