katya
katya

Reputation: 1

Stuck at Laravel , trying to save datas in database

I am doing this function to call it later, but what is saving in database are only first_name, password and email. There is null for user_create_id and user_update_id.

protected function create(array $data)
{
    return User::create([
        'first_name' => $data['first_name'],
        'password' => Hash::make($data['password']),
        'email' => $data['email'],
        'user_create_id'=> '0',
        'user_update_id'=> '0',  ]);
}

Is there any way to save user_create_id and user_update_id?

Upvotes: 0

Views: 272

Answers (4)

whmkr
whmkr

Reputation: 3085

Without seeing your user model, you probably need to update the fillable property to include those attributes, something like:

protected $fillable = [
    'firstname',
    'password',
    'email',
    'user_create_id',
    'user_update_id'
];

Unless you've set a guarded property instead, in which case you'd just want to ensure that none of those attributes are in the guarded property as it acts as a black list where fillable acts as a while list for mass attribute assignment. A workable guarded version would look like this

protected $guarded = [];

Upvotes: 2

Ohgodwhy
Ohgodwhy

Reputation: 50798

Make sure these properties are fillable. Also make sure that the column type supports strings, as you're using '0' which is a string, not an integer:

Fillable:

// User model

protected $fillable = [
    'first_name',
    'password',
    'email',
    'user_create_id',
    'user_update_id'
];

Upvotes: 0

ceejayoz
ceejayoz

Reputation: 180177

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

You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.

Make sure user_create_id and user_update_id are in your app/User.php file's $fillable definition.

Upvotes: 0

Felippe Duarte
Felippe Duarte

Reputation: 15131

Make sure you have

protected $fillable = [
    ....
    'user_create_id'=> '0',
    'user_update_id',
];

Inside User class

Upvotes: 0

Related Questions