Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 216
ssuhat Avatar asked May 19 '15 07:05

ssuhat


People also ask

How disable updated at Laravel?

By setting the class constant of UPDATED_AT to null on the models you wish to disable that column, Laravel will no longer attempt to set the column automatically.

How to disable timestamp in Laravel?

Update: You can also disable timestamps by removing $table->timestamps() from your migration. Save this answer.

What is update in laravel?

The recommended method of upgrading is to create a new Laravel 5.0 install and then to copy your 4.2 site's unique application files into the new application. This would include controllers, routes, Eloquent models, Artisan commands, assets, and other code specific to your application.


4 Answers

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;
}
like image 99
Akar Avatar answered Oct 23 '22 23:10

Akar


In your model, add this method:

/**
 * @param  mixed  $value
 * @return $this
 */
public function setUpdatedAt($value)
{
    return $this;
}

UPDATE: In Laravel 5.5:

Just try to use this in your model:

const CREATED_AT = null;
const UPDATED_AT = null;
like image 40
Bald Avatar answered Oct 23 '22 21:10

Bald


The accepted answer didn't work for me, but led me in the right direction to this solution that did:

class Whatever extends Model {
    //...
    const UPDATED_AT=NULL;
    //...

Laravel 5.3

like image 27
chiliNUT Avatar answered Oct 23 '22 22:10

chiliNUT


In this case it's better to use Query Builder instead of Eloquent because Query Builder doesn't implicitely edits timestamps fields. The use of Query Builder will have the advantage of targetting only the concerned update operation without alterate all your model.

In one line you could do:

$query = \DB::table('stocklogs')->where('stock_id', $id)->whereBetween('created_at', [$from, $to])->update(['batch_id' => $max + 1]);
like image 38
Evrard-c Avatar answered Oct 23 '22 21:10

Evrard-c