1

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

1 Answer 1

3

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.

5
  • I tried it.. I believe but its is throwing a mass assignment error
    – Abhijith
    Aug 1, 2014 at 20:20
  • Ok, I've updated my answer with how you can set the fillable fields of your model to make them mass-assignable.
    – lowerends
    Aug 1, 2014 at 20:23
  • So I have to do that to all my tables, Are there any other advantages of create() over insert() other than this?
    – Abhijith
    Aug 1, 2014 at 20:28
  • That's indeed strongly recommended. Using create() also means you can use events like creating or created in your application to hook into and add some logic, which can be very useful.
    – lowerends
    Aug 1, 2014 at 20:32
  • 2
    create() is a Eloquent Model method, insert() is inherited from a query builder, thus it does not do anything beside a insert query to the database. Aug 2, 2014 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.