Default User model throws error in laravel

2019-04-16 15:54发布

问题:

The default model "User" in laravel throws error when I tried to use it. What I tried is,

$user = new User();
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->name = "Blah Blah";
$user->access_type = "admin";
$user->access_status = 1;
$user->save();

and the error thrown is

Symfony \ Component \ Debug \ Exception \ FatalErrorException

Call to undefined method User::save()

What is the issue? I also tried User::all() to retrieve the values, which also throws error Call to undefined method User::all().

Update1: Here is my model

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

class User extends Eloquent implements UserInterface, RemindableInterface {

    /**
     * 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 token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->remember_token;
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_token';
    }

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

}

Update2: I tried writing another model names and class name as Users, which works perfectly. But for authentication, it must be the User table right?

回答1:

The reason for the issue is autoloader load wrong "User" model. Please have a look at the "/vendor/composer/autoload_classmap.php" file. Inside the return array value for the key "User" must be $baseDir . '/app/models/User.php

return array(
...,
'User' => $baseDir . '/app/models/User.php',
...,
);


回答2:

Maybe User is a reserved word, just give the model another name say UserTbl



回答3:

Still didn't received any answer to my question. Anyways now I'm running it using the new model named Users with the same doubt in mind.