Laravel 5.5类型错误:传递给Illuminate \ Auth \ EloquentUserProvider :: validateCredentials()的参数1必须是一个实例

时间:2018-05-22 08:30:17

标签: laravel laravel-5

在Auth下的LoginController中,我使用了以下代码:

namespace App\Http\Controllers\Auth;

use App\Model\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Hash;
use Auth;
use DB;
use App\Model\UserAdmin;

class LoginController extends Controller {
use AuthenticatesUsers;
public function __construct() {
        $this->middleware('guest')->except('logout');
    }

public function doLogin(Request $request) {
$userdata = array(
            'email' => Input::get('email'),
            'password' => Input::get('password'),
            'status' => '1',
        );
if (Auth::guard('admin')->attempt($userdata)) {
  return Redirect::intended('/administrator/dashboard')->with('successMessage', 'You have successfully logged in.');
}
}
}

在App / Model下的UserAdmin(模型)中如下:

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Config;
class UserAdmin extends Authenticatable {
protected $table = 'adminusers';
    public $timestamps = false;
    protected $fillable = ['firstName', 'lastName', 'email', 'company', 'website'];

    public function __construct() {
        parent::__construct(); // Don't forget this, you'll never know what's being done in the constructor of the parent class you extended
    }

}

提交登录详细信息后,它会显示错误:

  

类型错误:传递给Illuminate \ Auth \ EloquentUserProvider :: validateCredentials()的参数1必须是Illuminate \ Contracts \ Auth \ Authenticatable的实例,App / Model \ UserAdmin的实例,在/ var / www / html中调用第379行/XXXXXX/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php

7 个答案:

答案 0 :(得分:10)

我认为您需要将implements \ Illuminate \ Contracts \ Auth \ Authenticatable添加到UserAdmin模型类定义中。

    class UserAdmin extends Model implements 
    \Illuminate\Contracts\Auth\Authenticatable

答案 1 :(得分:1)

尝试运行“ composer dump-autoload”以检查“模棱两可的用户类解析”。您可能有两个定义为“用户类”的类。

答案 2 :(得分:0)

您必须声明对Authenticateable接口使用AuthenticableTrait。

例如:

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Database\Eloquent\Model;

class Company extends Model implements Authenticatable
{
use AuthenticableTrait;

答案 3 :(得分:0)

  

使用Illuminate \ Foundation \ Auth \ AuthenticatesUsers;

然后在模型类中扩展AuthenticatesUsers而不是Model

答案 4 :(得分:0)

您必须在用户模型中使用Authenticateable

例如:

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
  //your code
}

答案 5 :(得分:0)

您必须扩展Authenticateable类并在用户模型中实现JWTSubject

例如:

class User extends Authenticatable implements JWTSubject {

答案 6 :(得分:0)

转到模型,而不是扩展模型,而是扩展用户

<?php

namespace App;


class Staff extends \Illuminate\Foundation\Auth\User
{
}