tuscan88
tuscan88

Reputation: 5839

User registration fields not being saved in Laravel 5

I am using Laravel 5 and have amended the default registrar a bit to use first_name and last_name instead of just name. For some reason though when the user is created the first_name and last_name fields are blank in the database. This is the code I am using:

public function validator(array $data)
{
    return Validator::make($data, [
        'first_name' => 'required|max:255',
        'last_name' => 'required|max:255',
        'discount_code' => 'max:255',
        'register_email' => 'required|email|confirmed|max:255|unique:users,email',
        'register_password' => 'required|confirmed|min:6|max:60'
    ]);
}

public function create(array $data)
{
    return User::create([
        'first_name' => $data['first_name'],
        'last_name' => $data['last_name'],
        'email' => $data['register_email'],
        'password' => bcrypt($data['register_password'])
    ]);
}

In the create method if I do a print_r($data) before User::create is called first_name and last_name are there but when the entry is stored to my users table in the database only the email and password get stored. Here is my table definition also:

CREATE TABLE IF NOT EXISTS `users` (
    `id` int(10) unsigned NOT NULL,
    `first_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `last_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
    `discount_code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
    `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Upvotes: 1

Views: 1786

Answers (1)

Joel Hinz
Joel Hinz

Reputation: 25414

The first_name and last_name fields need to be mass assignable in the model through the $fillable/$guarded arrays.

Upvotes: 6

Related Questions