Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update without touching timestamps (Laravel)

Is it possible to update a user without touching the timestamps?

I don't want to disable the timestamps completly..

grtz

like image 765
Jannick Vandaele Avatar asked Sep 19 '13 21:09

Jannick Vandaele


5 Answers

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.

like image 195
Antonio Carlos Ribeiro Avatar answered Oct 17 '22 01:10

Antonio Carlos Ribeiro


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.

like image 36
Mike Avatar answered Oct 17 '22 01:10

Mike


Above samples works cool, but only for single object (only one row per time).

This is easy way how to temporarily disable timestamps if you want to update whole collection.

class Order extends Model
{

 ....

    public function scopeWithoutTimestamps()
    {
        $this->timestamps = false;
        return $this;
    }

}

Now you can simply call something like this:

Order::withoutTimestamps()->leftJoin('customer_products','customer_products.order_id','=','orders.order_id')->update(array('orders.customer_product_id' => \DB::raw('customer_products.id')));
like image 21
Maksim Martianov Avatar answered Oct 17 '22 02:10

Maksim Martianov


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.

like image 18
azngunit81 Avatar answered Oct 17 '22 01:10

azngunit81


For Laravel 5.x users who are trying to perform a Model::update() call, to make it work you can use

Model::where('example', $data)
     ->update([
       'firstValue' => $newValue,
       'updatedAt' => \DB::raw('updatedAt')
     ]);

As the Model::update function does not take a second argument anymore. ref: laravel 5.0 api

Tested and working on version 5.2.

like image 14
hilnius Avatar answered Oct 17 '22 02:10

hilnius