laravel 类 App\User 包含 6 个抽象方法,因此必须声明为抽象方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41891898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:12:56  来源:igfitidea点击:

Class App\User contains 6 abstract methods and must therefore be declared abstract

laravel

提问by Rock

I saw this error for the first time and don't really know what to do about it. When I tried to register a new user on my website and when I clicked on submit button it shows:

我第一次看到这个错误,真的不知道该怎么办。当我尝试在我的网站上注册新用户并单击提交按钮时,它显示:

FatalErrorException in User.php line 11: Class App\User contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifierName, Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifier, Illuminate\Contracts\Auth\Authenticatable::getAuthPassword, ...)

FatalErrorException in User.php line 11: Class App\User 包含 6 个抽象方法,因此必须声明为抽象方法或实现其余方法 (Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifierName, Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifier, Illuminate\Contracts\Auth\Authenticatable::getAuthPassword, ...)

User Model:

用户模型:

<?php 
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements Authenticatable
{
protected $table = 'users';
protected $primaryKey = 'id';   
}

What is it trying to say, I don't understand. Please can someone help me with this?

它想说什么,我不明白。请有人帮我解决这个问题吗?

回答by zgabievi

You are implementing Illuminate\Contracts\Auth\Authenticatable. This is interface and requires your Userclass to have some required methods:

您正在实施Illuminate\Contracts\Auth\Authenticatable. 这是接口,需要您的User类具有一些必需的方法:

public function getAuthIdentifierName();
public function getAuthIdentifier();
public function getAuthPassword();
public function getRememberToken();
public function setRememberToken($value);
public function getRememberTokenName();

If you are trying to make default Usermodel, you should use Illuminate\Foundation\Auth\User as Authenticatableand extend it instead of Modelclass. There is no need to implement Authenticatableinterfase.

如果您尝试创建默认User模型,则应使用Illuminate\Foundation\Auth\User as Authenticatable并扩展它而不是Model类。不需要实现接口Authenticatable

回答by Avik Aghajanyan

You need to either extend Illuminate\Foundation\Auth\Userinstead of Illuminate\Database\Eloquent\Model, or use Illuminate\Auth\Authenticatabletrait in your class

您需要在类中扩展Illuminate\Foundation\Auth\User而不是Illuminate\Database\Eloquent\Model使用Illuminate\Auth\Authenticatabletrait

UPDATE

更新

You need to extend Illuminate\Foundation\Auth\User like this

你需要像这样扩展 Illuminate\Foundation\Auth\User

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

} 

UPDATE 2

更新 2

Also make sure you don't have native Laravel's App\User model in you app folder, named User.php

还要确保您的应用程序文件夹中没有本地 Laravel 的 App\User 模型,名为 User.php