Php – Laravel – Disable Updated At when updating


Get a problem with update using query builder on laravel 5. I've tried to disabled the updated_at but keep failing.

Here is my code:

    $query = StockLog::where('stock_id', $id)->whereBetween('created_at', $from, $to])->update(['batch_id' => $max + 1]);

I've tried 2 ways:
first one at my model i set:

public function setUpdatedAtAttribute($value)
    /*do nothing*/

Second one:

$stocklog = new StockLog;
$stocklog->timestamps = false;

$query = $stocklog::where('stock_id', $id)->whereBetween('created_at', [$from, $to])->update([
        'batch_id' => $max + 1]);

both of them are failed. is there anyway to disabled the updated_at?

Thanks in advance

Best Solution

By default, Eloquent will maintain the created_at and updated_at columns on your database table automatically. Simply add these timestamp columns to your table and Eloquent will take care of the rest.

I don't really suggest removing them. But if you want use the following way.

add the following to your model:

public $timestamps = false;

This will disable the timestamps.

EDIT: it looks like you want to keep the created_at field, you can override the getUpdatedAtColumn in your model.

Use the following code:

public function getUpdatedAtColumn() {
    return null;