1

I modified the register controller AND the user table to have a role attribute as a string:

Schema::table('users', function (Blueprint $table) {
        $table->string('role')->nullable()->index();
    });

and i have created an enums.php in config folder like this:

return [
'role_types' => [
    'ADMIN' => "Admin",
    'TEACHER' => "Teacher",
    'STUDENT' => "Student",
]];

on the register user view (which is scaffold by default with bootstrap) i made select box to accommodate the selection:

<select class="form-control" id="role" name='role'>
           @foreach (Config::get('enums.role_types') as $role)
                    <option value="{{ $role }}">{{$role}}</option>
           @endforeach
</select>

But up on registering the user, the value is NULL.. what am i doing wrong ?

4
  • Check the request on form submission, dd(request()); Commented Dec 28, 2016 at 19:14
  • the dd(request()); on the create method on the register controller returns the following: request: ParameterBag {#41 ▼ #parameters: array:6 [▼ "_token" => "OwStAMhDtUAOf5JS76mry2lVVA7Q0G5IE6et20EU" "name" => "teacher" "email" => "[email protected]" "role" => "Teacher" "password" => "123456" "password_confirmation" => "123456" ] Commented Dec 28, 2016 at 19:38
  • There is "role" => "Teacher". How did you save into database? Commented Dec 28, 2016 at 20:08
  • protected function create(array $data) { dd(request()); return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'role' => $data['role'], 'password' => bcrypt($data['password']), ]); } the controller and the table attribute as set from above. Commented Dec 28, 2016 at 20:13

1 Answer 1

2

When using Model::create() you have to set your model's fillable array:

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

Laravel does not let you mass assign a row using data it cannot trust. Check the docs: https://laravel.com/docs/5.3/eloquent

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much.. i have totally forgotten about fillable array in model.. That worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.