Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AdrianoMaia's avatar

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

0 likes
7 replies
bwrice's avatar

make sure you add "type" to your "fillable" attributes array on your User model.

3 likes
bwrice's avatar

Try the die-and-dumb method at the top of your create method and see what your request is getting

protected function create(array $data)
    {

    dd($data);

        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'type' =>$data['user'],
        ]);
    }
1 like
rcevs's avatar
rcevs
Best Answer
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".

ac-deathlock's avatar

I am new to laravel and doing same thing,i.e. trying to add new fields but getting errors "SQLSTATE[HY000]: General error: 1364 Field 'lname' doesn't have a default value (SQL: insert into users (name, email, password, updated_at, created_at) values (q, [email protected], $2y$10$6d.cg0u/YB8fkhM5gVxY3uye1zz4I8KyDRFkvnYoe6YzUMAoUttXy, 2017-06-15 07:28:57, 2017-06-15 07:28:57))"

"SQLSTATE[HY000]: General error: 1364 Field 'lname' doesn't have a default value"

lname is new field I am adding and I have already altered my table accordingly

hishamhadraoui's avatar

you should add the new field that you have added to the array of fillable it's in your USER model , you have to add it so it can add the field to your INSERT SQL, because what happend is like you dont have the new field in your sql query because you laravel doesn't know if it's fillable or not . and for that the field take null as a value and that's not acceptable by MYSQL so you got this kind of error

2 likes
aliharis's avatar

@AC-DEATHLOCK - If you're using the default mass-assign to create the user, you've to add the new field to $fillables array on User.php

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

Please or to participate in this conversation.