无法将管理员类型分配给我的create admin命令

时间:2018-11-06 19:07:49

标签: php sql laravel

我想创建一个将生成新管理员帐户的命令。该命令将询问一些详细信息。我想为该新帐户提供admin的类型字段,但是我收到错误消息,我的类型字段没有默认值。怎么了?

代码

用户模型

replace=FALSE

创建:管理员

set.seed(3)
l <- replicate(1e5, sample(unique(v), size=2, prob=table(v)))
any(l[1,] == l[2,])
# [1] FALSE

为普通用户注册控制器

class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;

    const ADMIN_TYPE = 'admin';
    const DEFAULT_TYPE = 'default';

    public function isAdmin() {

        return $this->type === self::ADMIN_TYPE;
    }
}

错误

public function handle()
{
    $name = $this->ask('What is your full name?');
    $username = $this->ask('What is your username? (this will be your display name)');
    $email = $this->ask('What is your email?');
    $country= $this->ask('Where do you live? (country)');
    $password = $this->secret('Choose a password');

    User::create([
        'name' => $name,
        'username' => $username,
        'email' => $email ,
        'password' => Hash::make($password),
        'country' => $country,
        'type' => User::ADMIN_TYPE,
        'participated' => false,
    ]);
}

2 个答案:

答案 0 :(得分:1)

修改您的用户类别,如下所示:

class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;

    const ADMIN_TYPE = 'admin';
    const DEFAULT_TYPE = 'default';

    protected $fillable = [
        'name', 'email', 'username', 'password', 'participated', 'type', 'country'
    ];

    public function isAdmin() {

        return $this->type === self::ADMIN_TYPE;
    }
}

我希望这会对您有所帮助。

答案 1 :(得分:0)

我以前曾遇到过这些问题,通常,这是用户模型中fillable属性的白名单错误。检查https://laravel.com/docs/5.7/eloquent#mass-assignment了解更多详细信息。

如果您已经将类型列入白名单,请转储User :: DEFAULT_TYPE。