Web Guard允许登录,但Admin Guard不允许登录

时间:2020-09-27 14:42:09

标签: laravel eloquent controller guard laravel-middleware

我定义了一个新的防护“ Admin”,以便在我的项目中具有多身份验证系统用户和admin。 Web Guard允许登录。但是Admin Guard不允许登录 当我尝试登录Admin时,它会提示

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'last_sign_in_at' in 'field list' (SQL: update `admins` set `updated_at` = 2020-09-27 12:49:24, `last_sign_in_at` = 2020-09-27 12:49:24, `current_sign_in_at` = 2020-09-27 12:49:24 where `id` = 1)

我的用户表

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('user_type');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->boolean('status')->default(0);
        $table->timestamp('last_sign_in_at')->nullable();
        $table->timestamp('current_sign_in_at')->nullable();
        $table->string('user_click');
        $table->timestamp('user_click_time')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });
}

我的管理表

  public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('user_type');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->boolean('status');
            $table->rememberToken();
            $table->timestamps();

             });
    }

auth.php

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

        //admin guard
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],





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

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

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],



 'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],

        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

我的中间件 CheckRole

  public function handle($request, Closure $next)
    {
        
        if (!Auth::guard('admin')->check()){
            return redirect('admin/login');
        }
        return $next($request);
        
    }

我的 Admin.php 模型

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
//guard
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
//guard End

class Admin extends Authenticatable
{

    use Notifiable;
    protected $guard ='admin';
    protected $hidden = [
        'password', 'remember_token',
    ];
    protected $guarded=[];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


}

我的 AdminController

   public function adminLogin(Request $request){
        if ($request->ismethod('post')) {
            $data = $request->input();

            if ( Auth::guard('admin')->attempt(['email' => $data['email'], 'password' => $data['password'],
                'user_type'=>'admin', 'status' => '1'])){
                return view('admin.dashboard');
            }
            else {

                return back()->with('error',' Invalid UserName Or Password');
            }

        }

    }

当我尝试登录Admin时,出现错误。任何解决方法ps!

1 个答案:

答案 0 :(得分:0)

似乎您有一个事件侦听器在监听Auth的LoginEvent,并且正在模型上设置last_sign_in_at字段并保存。由于您使用不同的模型进行身份验证,因此最终将尝试根据该事件中的模型进行此操作。在这种情况下是Admin模型。

您将需要将此字段添加到管理表中,或者必须在侦听器中检查事件持有的模型,并根据模型的类型决定是否更新此字段。