简体   繁体   中英

Laravel Eloquent

When I try to fill my database using the model method create like this:

public function registerDevice($command) {
    $deviceId = $command->deviceId;
    $deviceToken = $command->deviceToken;

    $this->deviceId = $deviceId;
    $this->deviceToken = $deviceToken;

    Device::create(array('device_id' => $deviceId, 'device_token' => $deviceToken));
    $this->raise(new DeviceWasRegistered($this));
    return $this;
}

The entry is being made, but only the timestamps are being updates. The value fields are empty. No error coming up or something else is failing. But the values I want to put into the db are there if I var_dump the variables.

Do I miss something out?

In order for the create method to work, you need to put your two fields in the $fillable array on the model. So make sure you have this in your model:

protected $fillable = [
    'device_id',
    'device_token',
];

You can read more about the create method and mass assignment at http://laravel.com/docs/5.1/eloquent#mass-assignment .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM