How to check additional field during login in Laravel?

Viewed 2894

How to check additional field during login in Laravel?

Now it works from the box and accepts email and password. How to check additionally status parameter when user log in?

I use this LoginController:

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
   use AuthenticatesUsers;
}

I tried this in LoginController:

public function authenticate(Request $request)
    {
        $credentials = $request->only('email', 'password');
        $credentials['status'] = 1;

        if (Auth::attempt($credentials)) {
            return redirect()->intended('home');
        }
    }

But Laravel ignores this method authenticate().

if to remove use AuthenticatesUsers; it becomes alive, but then does not work logout, login page.

5 Answers

You can add another field to check login by credentials function

inside you LoginController add credentials function and append extra field with email, password

Example:

public function credentials(Request $request)
    {
        $credentials = $request->only($this->email(), 'password');
        $credentials = array_add($credentials, 'status', '1');
        return $credentials;
    } 

please add below code in your LoginController..

public function credentials(Request $request)
{

 return array_merge($request->only($this->username(), 'password'), ['type' => 'USER']);

} 

Your question tag is laravel 5.2, then for 5.2 you should override getCredentials function:

protected function getCredentials(Request $request)
{
    $credentials = $request->only($this->loginUsername(), 'password');

    $credentials['status'] = 1;

    return $credentials;
}

if you using laravel 5.3+ the answer of Emtiaz Zahid is correct (override credentials function)

In your LoginController add an extra field in the following function

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

In your LoginController.php

 Illuminate\Http\Request;

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