make sure you add "type" to your "fillable" attributes array on your User model.
Mar 26, 2017
7
Level 1
Laravel 5.4 Add another field to auth (register page)
How can add two check boxes to the register page, created automatically with laravel 5.4, and pass their value to the database?
I do the following steps and doesn't work :
First add this code, to register.blade.php file:
<div class="form-group{{ $errors->has('type') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Tipo Utilizador</label>
<div class="col-md-3">
<label for="user" class="radio-inline">Admin Sistema</label>
<input type="radio" id="user" checked="checked" class="form-control" name="admin" value="sysAdmin">
</div>
<div class="col-md-3">
<label for="user" class="radio-inline"> Admin</label>
<input type="radio" id="user" class="form-control" name="admin" value="admin">
</div>
</div>
Next I change the Register Controller:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'type' => 'required|max:255'
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'type' =>$data['user'],
]);
}
}
In the last step, I change the migration, and migrate her to the database:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id_users');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('type');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
The problem is, when I try to register a new user, the checkbox value is null.
Thanks
Level 1
There's no input named "type" in your form. The input's name is "admin".
Try changing your html so your radio inputs' name are "type".
Please or to participate in this conversation.