Laravel - Disable Updated At when updating
Asked Answered
N

7

19

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

Nailbrush answered 19/5, 2015 at 7:59 Comment(12)
As in permanently turn it off? Or just for one specific query?Fellini
Hi. Permanent off. @TheShiftExchangeNailbrush
And when you say "both of them are failed" - what is the actual fail/error?Fellini
Column not found: 1054 Unknown column 'updated_at' in 'field list' @TheShiftExchangeNailbrush
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 manualyOffstage
@sstarlight You can disable it in model, check my answer.Karyosome
@sstarlight laravel always expects the updated_at and created_at unless you have told it that it does not existsOffstage
@Karyosome i've tried. i comment your answer. THanksNailbrush
@Offstage Yes. i've tried to disabled it. but no luck. any suggestion?Nailbrush
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?Offstage
My table not using any migrations.Nailbrush
@sstarlight Oh yea then my suggestion is not going to help you :(Offstage
P
32

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;
}
Pennyweight answered 19/5, 2015 at 8:19 Comment(2)
i need the created at for other query.Nailbrush
ouch!! it's success~. Thanks a lot.Nailbrush
F
13

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;
Finite answered 17/6, 2016 at 10:14 Comment(0)
I
10

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

Iaria answered 19/10, 2017 at 16:37 Comment(2)
I believe this is the best answer to the question. from the source: if (! is_null(static::UPDATED_AT) && ...) { $this->setUpdatedAt($time); }Zen
@Zen Thanks! And thanks for providing the source that backs it upIaria
A
6

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]);
Apomict answered 16/9, 2017 at 13:18 Comment(0)
K
3

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;
}
Karyosome answered 19/5, 2015 at 8:4 Comment(2)
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'Nailbrush
Check this laravel.com/docs/5.0/eloquent#timestamps there is a way to block timestamps.Karyosome
E
0

Before updating you need to add ->toBase()

For example

Model::query()->where([...])->toBase()->update([...]);

in your case it will be

StockLog::where('stock_id', $id)->whereBetween('created_at', $from, $to])->toBase()->update(['batch_id' => $max + 1]);
Eisenstark answered 26/1, 2023 at 16:25 Comment(0)
B
0

in laravel 7 in the model:

    public    $timestamps = ['created_at'];
    const     UPDATED_AT  = null;

for example:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class table extends Model
{
    protected $table      = 'table';
    public    $timestamps = ['created_at'];
    const     UPDATED_AT  = null;
//...

Bilingual answered 16/5, 2024 at 9:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.