Laravel Eloquent increment without updating timestamps

laravel increment
laravel update increment
laravel increment not working
laravel timestamps
laravel eloquent update
laravel increment column on update
laravel increment value
laravel update without timestamps

I have an Eloquent model on which I would like to increment a single attribute. So far I've been using the following line of code to achieve this:

Thread::where('id', $threadId)->increment('like_count');

This however has the unwanted side-effect of updating the updated_at timestamp. I've found the following way of updating a record without altering the timestamp:

    $thread = Thread::where('id', $threadId)->first();

    $thread->timestamps = false;
    $thread->like_count++;
    $thread->save();

But that suddenly looks a lot less concise. Therefore, I would like to know of there's a way to use the increment method without updating timestamps.

If you do not need timestamps at all, you can disable it once for all for that particular model using :

public $timestamps = false; inside your model. This will add additional step that whenever you want the timestamps to be updated, you need to assign them value manually like $object->created_at = Carbon::now()

Secondly, if you want those disabled for particular query, then as you mentioned in your question is one way.

Another way is using query builder. Now timestamps is the functionality associated with Eloquent. However, if you update using simple query builder, it does not update timestamps on its own.

So you can do :

DB::table('threads')
            ->where('id', $threadId)
            ->update([ 'votes' => DB::raw('votes + 1') ]);

However, I will personally prefer using Eloquent way of doing this if given a choice.

Laravel eloquent model increment with or without updating timestamps, I have an Eloquent model on which I would like to increment a single attribute. So far I've been using the following line of code to achieve this: Thread::where('id'� Eloquent: incrementing columns without update() function July 20, 2015 Eloquent mechanism isn’t limited to just create/update/delete functions – that’s why it’s awesome.

You could encapsulate the whole process into one method of the model.

update() without updating timestamps, Problem: You may need to increment a field in a record of a table (a single attribute of a Laravel Model) and do not want update the updated_at� Laravel / Eloquent - primary id field incrementing issue Hot Network Questions How was the 1849 "Proclamation to the People of California" announcing the formation of a Constitution communicated to the people?

old thread but with laravel 7 and php7.4 you can do

Thread::where('id', $threadId)
->where(fn($q) => $q->getModel()->timestamps = false)
->increment('like_count');

older versions of php:

Thread::where('id', $threadId)
->where(function($q) {$q->getModel()->timestamps = false;})
->increment('like_count');

Eloquent: incrementing columns without update() function, How can I run that query without updating the timestamps? Is there any way to mass update, but not update the timestamps with Eloquent? ->update(['status' = > 'Unallocated'], ['timestamps' => false]); in a Laravel 5.3 app and it doesn't seem to In LR5.7 Increment view count without change updatet_at You can specify additional columns to update during the increment or decrement operation: Product::increment('count', 1, ['last_count_increased_at' => Carbon::now()]); More Information: The eloquent provides convenient methods for incrementing or decrementing the value of a given column. This is a shortcut:

8 Tricks with Laravel Timestamps, Eloquent: incrementing columns without update() function One of those helpers come to rescue when you need to increment a column, basically run update X set Y=Y+1 where id = Z 8 Tricks with Laravel Timestamps� The update method expects an array of column and value pairs representing the columns that should be updated. {note} When issuing a mass update via Eloquent, the saving, saved, updating, and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.

Eloquent: Getting Started - Laravel, To disable that automatic timestamps, in your Eloquent Model you need to add one property: Update without touching updated_at But sometimes you want to avoid it, like you increment some value and don't want to� Laravel / Eloquent doesn't support composite primary keys. When you check the Eloquent Model class, you can see that the primary key can only be a string, which is used to create the WHERE clause. share | improve this answer | follow |

Update without touching timestamps?, Note that we did not tell Eloquent which table to use for our Flight model. In addition, Eloquent assumes that the primary key is an incrementing integer value, Again, the updated_at timestamp will automatically be updated, so there is no� Each migration file name contains a timestamp, which allows Laravel to determine the order of the migrations. {tip} Migration stubs may be customized using stub publishing The --table and --create options may also be used to indicate the name of the table and whether or not the migration will be creating a new table.

Comments
  • You need the timestamps? You can disable them in your model.
  • Yes, I do need the timestamps to be updated on every query but this one.
  • I believe it's protected $timestamps = false; I wouldn't prefer internal variables of Model to be public. But, anyway, would work.
  • Its way Laravel documentation suggests here. It seems like its has to be public.
  • Ahan! It's been a while since I last did Laravel. Glad I stepped in, here :D
  • Its like this since Laravel 4.2 haha :D But I cant agree more that having it public rang a bell to me as well, I also double checked before posting in answer
  • Thanks for listing the options. I hoped there'd be a way to get it done via the increment method, but I'll stick to what I have then.