Filgard Filgard -4 years ago 57
MySQL Question

How do I fix user register on my new Laravel project

I created a new Laravel project. Used the standard
php atrisan make:auth
and that generated the required files and connections. I created the table in phpMyAdmin and conected the two in the .env file. I checked connections and migrations and all works well.

The problem is that when I want to create a new user and enter all the register data the fiels first_name and last_name apparently can not be read and I get an error saying "The first name field is required." same for last name. No error on password or email.

The code:

First of RegisterController

protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|string|max:20',
'last_name' => 'required|string|max:30',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}


register.blade.php

<form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}

<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
<label for="first_name" class="col-md-4 control-label">First Name</label>

<div class="col-md-6">
<input id="first_name" type="text" class="form-control" first_name="first_name" value="{{ old('first_name') }}" required autofocus>

@if ($errors->has('first_name'))
<span class="help-block">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
<label for="last_name" class="col-md-4 control-label">Last Name</label>

<div class="col-md-6">
<input id="last_name" type="text" class="form-control" last_name="last_name" value="{{ old('last_name') }}" required autofocus>

@if ($errors->has('last_name'))
<span class="help-block">
<strong>{{ $errors->first('last_name') }}</strong>
</span>
@endif
</div>
</div>


and migrations

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name', 20);
$table->string('last_name', 30);
$table->string('email')->unique();
$table->string('password', 30);
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}


I checked forums and Laravel documentation but couldn't define a mistake... Please help.

Answer Source

The issue is here:

<input id="first_name" type="text" class="form-control" first_name="first_name" value="{{ old('first_name') }}" required autofocus>

here instead of name="first_name", first_name="first_name" is there. Make it proper and try again. Same for last_name="last_name" also.

Explanation:

On the server side, element value is fetched using the name attribute of element. But in your case name attribute is missing which causes the validation to fails and it is showing The first name field is required and same for last_name

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download