简体   繁体   English

我在 Laravel 5.4 中为 Voyager 管理面板包创建管理员用户时遇到问题

[英]I have issue in creating admin user for Voyager Admin panel package in Laravel 5.4

after voyage package installation with this command :使用此命令安装 voyage 包后:

php artisan voyager:install

I needed to create admin user with this command :我需要使用以下命令创建管理员用户:

php artisan voyager:admin your@email.com --create

and then entering the name and the password然后输入名称和密码

The problem is after entering the username and the password it's returning this error问题是输入用户名和密码后返回此错误

General error: 1364 Field 'employee_name' doesn't have a default value (SQL: insert into employee ( email ) values (admin@admin.com))一般错误:1364 字段“employee_name”没有默认值(SQL:插入到employeeemail )值(admin@admin.com))

and I have already a database but with different naming convention for example: the users table I call it employee and the name field called employee_name我已经有一个数据库,但具有不同的命名约定,例如:我称之为员工的用户表和名为 employee_name 的名称字段

My question is , is there any way to map the expected fields in voyager with my actual fields in my database because I can't change them ?我的问题是,有没有办法将 voyager 中的预期字段与我数据库中的实际字段进行映射,因为我无法更改它们?

Thanks in advance .提前致谢。

Since you altered the normal structure of the User model and will likely run into similar issues with other packages that try to add users, you could create a boot() method in the user model to set this->employee_name to this->name and then unset this->name so that when records from other packages try to use the name attribute you correct it to the employee_name attribute.由于您更改了 User 模型的正常结构,并且可能会遇到与尝试添加用户的其他软件包类似的问题,您可以在用户模型中创建一个 boot() 方法,将 this->employee_name 设置为 this->name 和然后取消设置 this->name 以便当来自其他包的记录尝试使用 name 属性时,您将其更正为 employee_name 属性。

This is off the top of my head based on memory so it may involve a bit more 'logic' but I think if you look into the boot method you will see that you can do this type of thing.这是基于内存的我的头脑,所以它可能涉及更多的“逻辑”,但我认为如果你查看引导方法,你会发现你可以做这种类型的事情。 I have used it to autofill columns with dynamically based values.我用它来自动填充基于动态值的列。

HTH HTH

When you're executing this command, Voyager creates a new user for you using the mass assignment feature.当您执行此命令时,Voyager 使用批量分配功能为您创建一个新用户

So, add $fillable to the User model:因此,将$fillable添加到User模型中:

protected $fillable = ['name', 'email', 'password'];

General error: 1364 Field 'employee_name' doesn't have a default value一般错误:1364 字段 'employee_name' 没有默认值

You can set default value to null in your database this solves the problem您可以在数据库中将默认值设置为 null 这解决了问题

Use Mutator to refer any attempt to add data to 'name' column to 'employee_name'.使用Mutator将任何将数据添加到 'name' 列的尝试引用到 'employee_name'。

For your case:对于您的情况:

App\\User.php应用\\用户.php

public function setNameAttribute($value)
{
    $this->attributes['employee_name'] = $value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM