-1

I'm presently learning Laravel and Eloquent, and have set up some initial migrations to play with. One of my tables only needs a creation time, since once a row is inserted there, it will never be updated:

    // The run table only needs a creation timestamp, not an updated timestamp
    Schema::create('runs', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamp('created_at');
    });

I understand that Eloquent by default expects both a created_at and an updated_at column to be present, and that this feature can be turned off entirely. From the manual:

By default, Eloquent expects created_at and updated_at columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps property on your model to false.

However, is it possible for Eloquent to be asked to automatically set a creation time and not an update time? I realise I can do this manually, but it would be nice if Eloquent could do this "for free".

halfer
  • 19,824
  • 17
  • 99
  • 186
  • [A simple solution](http://stackoverflow.com/q/18067614), which I am using for now, is to set the default column value at the database level, and turn off the timestamp feature in Eloquent. – halfer Oct 05 '15 at 20:26

2 Answers2

2

Short answer: no.

I had a look at framework/src/Illuminate/Database/Eloquent/Model.php which handles the timestamps and it's not directly possible.

You could override a bunch of methods to make it work:

  • public function setUpdatedAt($value)
  • anywhere there's a reference to static::UPDATED_AT
  • anywhere that depends on the value of usesTimestamps()
  • and certainly other places

At best, this would be vulnerable to future code changes so I don't recommend it.

bernie
  • 9,820
  • 5
  • 62
  • 92
  • A nice straightforward answer, for which thanks! No problem - I'll stick with the manual approach. – halfer Oct 05 '15 at 21:12
0

you can just use

$model->touch();
Avenger
  • 51
  • 3