0

We are having a strange issue with Laravel 5 in that it is refusing to store the checkbox value.

We are adapting the existing registration form that comes bundled with Laravel 5 and we are adding an optin checkbox but it seems the model does not recognise this as a field even though we are adding it as a field in the migration file.

Any help on this would be appreciated.

Mirgration File:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->date('dob');
        $table->boolean('optin')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });
}

Then we add it to the register.blade.php file:

<div class="form-group">
                        <label class="col-md-4 control-label">Optin</label>
                        <div class="col-md-6">
                            <input type="checkbox" class="form-control" name="optin">
                        </div>
                    </div>

At the point of creating the User model, we check the value of the checkbox and assign it.

protected function create(array $data)
{

    //this does return 1 or 0 as expected
    $optin = ($data["optin"] == "on") ? 1 : 0; 

    return User::create([
        'first_name' => $data['first_name'],
        'last_name' => $data['last_name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'dob' => $data['dob'],
        'optin' => $optin
    ]);
}

But at this point the field is null. No value is entered into the database...

2
  • So $optin isn't even saved as 0? Commented Jun 17, 2015 at 10:03
  • No the field is null. Even tried in PHP tinker and it still refuses to add $user->optin = 1 to the user object but will let me assign other names! Tried to change the name however, and still does not actually add the value to the database! Commented Jun 17, 2015 at 10:07

2 Answers 2

2

Have you put the field 'optin' in the $fillable array within the model? Otherwise you cant create a User with 'optin' using the static create method.

//File: User.php
protected $fillable = ['optin'];
Sign up to request clarification or add additional context in comments.

1 Comment

0

The model already has a static create() function. Therefore, when you make a call like User::create($data) from your controller, your function is not called.

My approach is to change the name of your function and make it static.

Update

Also you can override the create function:

public static function create(array $attributes)
{
    $attributes["optin"] = ($attributes["optin"] == "on") ? 1 : 0;

    return parent::create($attributes);
}

7 Comments

But this is the AuthController.php that comes bundled with Laravel 5...?
Where is the point that you call the above function? I suppose the function is called by a controller like User::create($data).
It runs through the Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers trait that exists in the laravel vendor directory so it gets called through there...
Ohh now I understand what you want. I'll update my answer.
Thanks for your suggestion, however this now gives me an error. Call to undefined method App\Http\Controllers\Controller::create() in /Users/substance/Sites/big-bang-theory-fans-giving/app/Http/Controllers/Auth/AuthController.php on line 67
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.