7

after voyage package installation with this command :

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))

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

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 ?

Thanks in advance .

Ali Beshir
  • 412
  • 1
  • 6
  • 16

4 Answers4

1

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.

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

StevenHill
  • 320
  • 3
  • 8
0

When you're executing this command, Voyager creates a new user for you using the mass assignment feature.

So, add $fillable to the User model:

protected $fillable = ['name', 'email', 'password'];
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • first thanks for your answer .. but I think there is Misunderstanding .. the voyager package is creating new user and it expects a column called name .. that is not exists in my table .. the name column called employee_name in my table .. So it doesn't insert the row with employee_name column .. and the employee_name column doesn't have a default value so it throws that error – Ali Beshir Jan 09 '18 at 17:54
  • here is what I did in my model as you suggested protected $fillable = [ 'email', 'password','employee_name' ]; – Ali Beshir Jan 09 '18 at 17:54
  • @AliReda why don't you rename `employee_name` to `name`? – Alexey Mezenin Jan 09 '18 at 17:56
  • SQLSTATE[HY000]: General error: 1364 Field 'employee_name' doesn't have a default value (SQL: insert into `employee` (`email`, `password`) values (admin@admin.com, hashedPassword)) – Ali Beshir Jan 09 '18 at 17:56
  • because this I have to change that in many sql statements in a lot of controllers .. and there is an other apps that works with this database so I can't change the table name or it's columns – Ali Beshir Jan 09 '18 at 17:57
  • 1
    @AliReda in this case, you'll need to manually add the data with seeder or something because `name` is hardcoded in the command. – Alexey Mezenin Jan 09 '18 at 18:01
  • 1
    I think this is a logical and possible solution .. Thank you for your Time – Ali Beshir Jan 09 '18 at 18:16
0

General error: 1364 Field 'employee_name' doesn't have a default value

You can set default value to null in your database this solves the problem

PHPFan
  • 756
  • 3
  • 12
  • 46
0

Use Mutator to refer any attempt to add data to 'name' column to 'employee_name'.

For your case:

App\User.php

public function setNameAttribute($value)
{
    $this->attributes['employee_name'] = $value;
}
Preston Ong
  • 61
  • 1
  • 3