Kr1
Kr1

Reputation: 1303

Why all the fields are correctly stored except one?

I am creating a Provider like this:

$provider = Provider::create([
    'first_name' => $data['first_name'],
    'last_name' => $data['last_name'],
    'email' => $data['email'],
    'password' => bcrypt($data['password']),
    'is_available' => 1,
    'is_activated' => 1,
    'insurance' => 1
]);

Log::info($provider);

The Provider is actually well created, and all fields are stored correctly except the insurance field.

The insurance field isn't working and I can't figure out why.

It was created with Laravel migration tools, like this:

class AddInsuranceToProvidersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('providers', function($table) {
            $table->integer('insurance');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

And then from terminal:

php artisan migrate
php artisan cache:clear 

And here is how it looks like from phpMyAdmin:

enter image description here

Any idea why I can't save any value to insurance field?

Also when I log the $provider variable, there is nothing related with insurance field, as you can see there:

[2017-12-13 12:36:54] local.INFO: {"first_name":"fef","last_name":"ewfe","email":"[email protected]","is_available":1,"is_activated":1,"updated_at":"2017-12-13 12:36:54","created_at":"2017-12-13 12:36:54","id":276}


Thanks to the answers, I was able to solve the issue by adding the insurance field to Provider Model, as it looks like this now:

protected $fillable = [
        'name','first_name','last_name', 'email', 'password','is_available','is_activated','insurance'
    ];

Upvotes: 1

Views: 69

Answers (3)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Since you're using create() method, you need to add the field to $fillable array:

protected $fillable = ['insurance', ....];

You need to do that since create() is using fill() method to fill model fields for you.

https://laravel.com/docs/5.5/eloquent#mass-assignment

Upvotes: 1

Prateik Darji
Prateik Darji

Reputation: 2317

As you have altered table using migration you just need to update InsuranceProvider model in app directory and add insurance field to the fillable array in order to make this working.

Upvotes: 1

Amarnasan
Amarnasan

Reputation: 15529

If you set a "fillable" array in your model, you must add the "insurance" field to it.

Upvotes: 1

Related Questions