Update without touching timestamps (Laravel)

Solution 1:

Disable it temporarily:

$user = User::find(1);
$user->timestamps = false;
$user->age = 72;
$user->save();

You can optionally re-enable them after saving.

This is a Laravel 4 and 5 only feature and does not apply to Laravel 3.

Solution 2:

In Laravel 5.2, you can set the public field $timestamps to false like this:

$user->timestamps = false;
$user->name = 'new name';
$user->save();

Or you can pass the options as a parameter of the save() function :

$user->name = 'new name';
$user->save(['timestamps' => false]);

For a deeper understanding of how it works, you can have a look at the class \Illuminate\Database\Eloquent\Model, in the method performUpdate(Builder $query, array $options = []) :

protected function performUpdate(Builder $query, array $options = [])
    // [...]

    // First we need to create a fresh query instance and touch the creation and
    // update timestamp on the model which are maintained by us for developer
    // convenience. Then we will just continue saving the model instances.
    if ($this->timestamps && Arr::get($options, 'timestamps', true)) {
        $this->updateTimestamps();
    }

    // [...]

The timestamps fields are updated only if the public property timestamps equals true or Arr::get($options, 'timestamps', true) returns true (which it does by default if the $options array does not contain the key timestamps).

As soon as one of these two returns false, the timestamps fields are not updated.

Solution 3:

To add to Antonio Carlos Ribeiro's answer

If your code requires timestamps de-activation more than 50% of the time - maybe you should disable the auto update and manually access it.

In eloquent when you extend the eloquent model you can disable timestamp by putting

UPDATE

public $timestamps = false;

inside your model.