Laravel unexpected error “class user contains 3 abstract methods…”

前端 未结 8 566
野趣味
野趣味 2021-01-30 01:56

While programming my Authentication app on Laravel, I came across to an error I\'ve never seen before. I\'ve been brainstorming for almost an hour for the cause of this problem

相关标签:
8条回答
  • 2021-01-30 02:36

    "First, add a new, nullable remember_token of VARCHAR(100), TEXT, or equivalent to your users table. which is put like this $table->text('remember_token', 100)->nullable(); into the table then php artisan migrate. and update to your Elq model too.

    0 讨论(0)
  • 2021-01-30 02:37

    This is an update problem. Laravel force us to update the User.php model with the following code to fix this problem.

    Note: All existing "remember me" sessions will be invalidated by this change, so all users will be forced to re-authenticate with your application.

    public function getRememberToken()
    {
        return $this->remember_token;   
    }
    
    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    } 
    
    public function getRememberTokenName()
    {
        return 'remember_token';
    }
    
    0 讨论(0)
  • 2021-01-30 02:40

    I fixed the error by adding UserTrait to User.php. Get the original User.php file here. If you don't need the rememberMe stuff, you just need this:

    <?php
    
    use Illuminate\Auth\UserTrait;
    use Illuminate\Auth\UserInterface;
    
    class User extends Eloquent implements UserInterface {
    
        use UserTrait;
    
    
    }
    
    0 讨论(0)
  • 2021-01-30 02:40

    Class User contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods

    (Illuminate\Auth\UserInterface::getAuthIdentifier,

    Illuminate\Auth\UserInterface::getAuthPassword,

    Illuminate\Auth\UserInterface::getRememberToken, ...)

    0 讨论(0)
  • 2021-01-30 02:42

    I also get this error.then i added:

    getRememberToken()
    setRememberToken($value)
    getRememberTokenName()
    

    here after code is working

    0 讨论(0)
  • 2021-01-30 02:45

    I'm not a pro at implementing PHP Interfaces, but I believe you need to include all methods of UserInterface and RemindableInterface in your User class (since it implements them). Otherwise, the class is "abstract" and must be defined as such.

    By my knowledge, a PHP interface is a set of guidelines that a class must follow. For instance, you can have a general interface for a specific database table. It would include definition of methods like getRow(), insertRow(), deleteRow(), updateColumn(), etc. Then you can use this interface to make multiple different classes for different database types (MySQL, PostgreSQL, Redis), but they must all follow the rules of the interface. This makes migration easier, since you know no matter which database driver you are using to retrieve data from a table it will always implement the same methods defined in your interface (in other words, abstracting the database-specific logic from the class).

    3 possible fixes, as far as I know:

    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
    }
    

    I think #2 is best for you, since if your class doesn't implement all methods from UserInterface and RemindableInterface why would you need to say it does.

    0 讨论(0)
提交回复
热议问题