Laravel 意外错误“类用户包含 3 个抽象方法...”

2022-08-30 08:37:32

在Laravel上编写我的身份验证应用程序时,我遇到了一个我以前从未见过的错误。我已经为这个问题的原因集思广益了将近一个小时,但我找不到解决方案。

错误:

类 User 包含 3 个抽象方法,因此必须声明为抽象或实现其余方法(Illuminate\Auth\UserInterface::getRememberToken, Illuminate\Auth\UserInterface::setRememberToken, Illuminate\Auth\UserInterface::getRememberTokenName)

用户.php型号:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

protected $fillable = [
    "email",
    "username",
    "password",
    "password_temp",
    "code",
    "active",
    "created_at",
    "updated_at",
    "banned"
];

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

}

和寄存器控制器.php

<?php

class RegisterController extends BaseController {

public function getRegister()
{
    return View::make('template.home.register');
}

public function postRegister()
{
    $rules = [
        "email"         => "required|email|max:50|unique:users",
        "username"      => "required|max:50|min:5|unique:users",
        "password"      => "required|max:50|min:6",
        "password_again"=> "required|same:password",
    ];

    $messages = ["required" => "This field is required." ];

    $validator = Validator::make(Input::all(), $rules, $messages);

    if($validator->fails())
    {
        return Redirect::route('register')->withErrors($validator)->withInput();
    } else {
        $email      = Input::get('email');
        $username   = Input::get('username');
        $password   = Input::get('password');
        $code       = str_random(60);

        $user = User::create([
            'email'         => $email,
            'username'      => $username,
            'password'      => Hash::make($password),
            'code'          => $code,
            'activated'     => 0,
            'banned'        => 0
        ]);

        if ($user)
        {
            Mail::send('template.email.activate', ['link' => URL::route('activate', $code), 'username' => $username], function($message) use ($user)
            {
                $message->to($user->email, $user->username)->subject('Account Registration');
            });

            return Redirect::route('register')->with('homeError', 'There was a problem creating your account.');
        }
    }
    return Redirect::route('register')->with('homeError', 'Account could not be created.');
}
}

答案 1

啊,找到了。

它显然记录了Laravel Update。

您可以查看Laravel文档以解决您的问题:

“首先,添加一个新的、可为 null 的 VARCHAR(100)、TEXT 或等效于您的 users 表的remember_token。

接下来,如果您使用的是 Eloquent 身份验证驱动程序,请使用以下三种方法更新 User 类:

public function getRememberToken()
{
    return $this->remember_token;
}

public function setRememberToken($value)
{
    $this->remember_token = $value;
}

public function getRememberTokenName()
{
    return 'remember_token';
}

"

有关更多详细信息,请参阅 http://laravel.com/docs/upgrade


答案 2

我不是实现PHP接口的专业人士,但我相信你需要在你的类中包含和的所有方法(因为它实现了它们)。否则,该类是“抽象的”,必须这样定义。UserInterfaceRemindableInterfaceUser

据我所知,PHP接口是一个类必须遵循的一组准则。例如,您可以为特定数据库表提供通用接口。它将包括诸如、 、 、 等方法的定义。然后,您可以使用此接口为不同的数据库类型(MySQL,PostgreSQL,Redis)创建多个不同的类,但它们都必须遵循接口的规则。这使得迁移更容易,因为您知道无论使用哪个数据库驱动程序从表中检索数据,它都将始终实现接口中定义的相同方法(换句话说,从类中抽象出特定于数据库的逻辑)。getRow()insertRow()deleteRow()updateColumn()

据我所知,有3种可能的修复方法:

abstract class User extends Eloquent implements UserInterface, RemindableInterface
{
}

class User extends Eloquent
{
}

class User extends Eloquent implements UserInterface, RemindableInterface
{
     // include all methods from UserInterFace and RemindableInterface
}

我认为#2最适合你,因为如果你的类没有实现所有的方法,为什么你需要说它实现了。UserInterfaceRemindableInterface


推荐