Abhijith
Abhijith

Reputation: 411

Model::insert() is not updating timestamps

I added the timestamps in the Migration(Schema) and the model

$table->timestamps();  //In the Schema

public $timestamps = true; //In the model

I made an array with all fields of the database and inserted into the database.

Checkin::insert($input);

In the table, it updates all the fields except the updated_at, created_at timestamps

I have another(default) model for Users and it updates the timestamps. In that model, I am using the ->save() method

Upvotes: 1

Views: 2116

Answers (1)

lowerends
lowerends

Reputation: 5267

Instead of insert, you should use create if you want the timestamps to be set:

Checkin::create($input);

You can also do

$checking = Checkin::create($input);

which will give you the just created checkin.

Make sure the fields are fillable by adding this to your model:

protected $fillable = array(
    'name',
    'field_1',
    'field_2',
    ... // all the fields you want to be mass-assignable here
    );

Also, there's no need to set public $timestamps = true; as that is the default in Laravel if you're using Eloquent models.

Upvotes: 3

Related Questions