Laravel - Disable Updated At when updating

29,211

Solution 1

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;
}

Solution 2

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;

Solution 3

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

Solution 4

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]);

Solution 5

You can use following if you want make it off permanently.

Add following to your model...

public $timestamps = false;

And if you want to keep using created_at, then add following.

    static::creating( function ($model) {
        $model->setCreatedAt($model->freshTimestamp());
    });

OR use following way...

/**
 * Set the value of the "updated at" attribute.
 *
 * @param  mixed  $value
 * @return void
 */
public function setUpdatedAt($value)
{
    $this->{static::UPDATED_AT} = $value;
}
Share:
29,211

Related videos on Youtube

ssuhat
Author by

ssuhat

Love to learn a new thing in programming world.

Updated on July 09, 2022

Comments

  • ssuhat
    ssuhat almost 2 years

    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

    • Laurence
      Laurence almost 9 years
      As in permanently turn it off? Or just for one specific query?
    • ssuhat
      ssuhat almost 9 years
      Hi. Permanent off. @TheShiftExchange
    • Laurence
      Laurence almost 9 years
      And when you say "both of them are failed" - what is the actual fail/error?
    • ssuhat
      ssuhat almost 9 years
      Column not found: 1054 Unknown column 'updated_at' in 'field list' @TheShiftExchange
    • Szenis
      Szenis almost 9 years
      If you dont want to have it at all then its better to not have it in your migration. If you are using migrations you probably have something like $table->timestamps(); What you could do is set this public $timestamps = false; in your model and ad the created_at manualy
    • vps
      vps almost 9 years
      @sstarlight You can disable it in model, check my answer.
    • Szenis
      Szenis almost 9 years
      @sstarlight laravel always expects the updated_at and created_at unless you have told it that it does not exists
    • ssuhat
      ssuhat almost 9 years
      @vps i've tried. i comment your answer. THanks
    • ssuhat
      ssuhat almost 9 years
      @Szenis Yes. i've tried to disabled it. but no luck. any suggestion?
    • Szenis
      Szenis almost 9 years
      public function up() { Schema::create('password_resets', function(Blueprint $table) { $table->string('email')->index(); $table->string('token')->index(); $table->timestamp('created_at'); }); } This is how my password reset migration looks it does not have any updated_at field. Could you show your migration?
    • ssuhat
      ssuhat almost 9 years
      My table not using any migrations.
    • Szenis
      Szenis almost 9 years
      @sstarlight Oh yea then my suggestion is not going to help you :(
  • ssuhat
    ssuhat almost 9 years
    I've tried first one and the second one. for the first it gives me a lot of red syntax. especially on static::creating second one not working. it still try to update 'updated_at'
  • vps
    vps almost 9 years
    Check this laravel.com/docs/5.0/eloquent#timestamps there is a way to block timestamps.
  • ssuhat
    ssuhat almost 9 years
    i need the created at for other query.
  • ssuhat
    ssuhat almost 9 years
    ouch!! it's success~. Thanks a lot.
  • andonovn
    andonovn about 6 years
    I believe this is the best answer to the question. from the source: if (! is_null(static::UPDATED_AT) && ...) { $this->setUpdatedAt($time); }
  • chiliNUT
    chiliNUT about 6 years
    @andonovn Thanks! And thanks for providing the source that backs it up