0
votes

i am trying to create a multiple authentication in Laravel 7 with custom guards and when i try to login am getting this error "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from admins where email = [email protected] and 0 is null limit 1)"

My auth.php in config

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
    'admin-api' => [
        'driver' => 'token',
        'provider' => 'admins',
    ],
],


 'providers' => [

    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
        
    ],
],

My adminLoginController

 <?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use auth;
class AdminLoginController extends 

Controlle

r
{
    public function __construct()
    {
          $this->middleware('guest:admin');
    }
    public function showLoginForm()
    {
        return view('auth.admin-login');

    }
    public function login(Request $request)
    {
        // validate the data 
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:6'
        ]);

    // attempt user to login
    if(auth::guard('admin')->attempt(['email' => $request->email, 'password'=> $request->password, $request->remember])){
        //if susscefull  redirect to the intended location
        return redirect()->intended(route('admin.dashboard'));
    }
    // if unsuccesfull return to the page they were

}

}

My Admin model

    <?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Admin extends Authenticatable
{
    use Notifiable;
    protected $guard = 'admin';
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password', 'job_title',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

}

LoginController

 <?php



  namespace App\Http\Controllers\Auth;
    
    use App\Http\Controllers\Controller;
    use App\Providers\RouteServiceProvider;
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    
    class LoginController extends Controller
    {
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}


 
2
What does your guard look like? - Repox
the remember me flag isn't part of the credentials passed to attempt; it is its own argument - lagbox
@Repox My admin guard is included in the auth.php config in the question - logicalrap
@lagbox when i try to remove the 'remember' in the attempt function its reloding me back to the login page without any error. - logicalrap
@lagbox i get your answer i removed the remember flag in the attempt credentials and it worked. Thanks - logicalrap

2 Answers

0
votes

The problem is somewhere in your password when you are passing it. select * from admins where email = [email protected] and 0 is null limit 1) I gues that you are getting this error when trying to log in. Check out Login controller and see the parameters that you are passing. Or drop the controller here so we can provide more info.

0
votes

The attempt function was supposed to be like this

  if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {            return redirect()->intended(route('admin.dashboard'));
}