38

Is it possible to update a timestamp (besides updated_at) and increment a column in one query? I obviously can

->increment('count')

and separately

->update(['last_count_increased_at' => Carbon::now()])

but is there an easy way to do both together.

Product::where('product_id', $product->id)
    ->update(['count'=> $count + 1, 'last_count_increased_at' => Carbon::now()];

Without having to query and get the count first?

3 Answers 3

73

You can specify additional columns to update during the increment or decrement operation:

Product::where('id',$id)
->increment('count', 1, ['increased_at' => Carbon::now()]);

It is more eloquent solution.

2
  • 3
    Thanks, I wasn't aware of this! Mar 4, 2019 at 15:08
  • 4
    This feels like the more eloquent solution.
    – Josh
    Mar 19, 2021 at 14:53
66

You can use the DB::raw method:

Product::where('product_id', $product->id)
    ->update([
      'count'=> DB::raw('count+1'), 
      'last_count_increased_at' => Carbon::now()
    ]);
4
  • Works great, can't believe I didn't think of that, was about to do the whole thing raw. Jun 6, 2016 at 20:24
  • What if we don't have a matching record? How to create if no record found, without having to query and get the count first? Apr 28, 2020 at 2:20
  • 1
    @AjjayArora While that really ought to be a new question, I'm not sure that can be done in a one-liner using just Eloquent. I think you actually would have to check if the product_id exists first.
    – aynber
    Apr 28, 2020 at 11:22
  • @aynber Thanks pal for replying. Apr 29, 2020 at 10:57
7

With Laravel 8 you can now achieve this in a single query to create or update on duplicate key.

$values = [
    'name' => 'Something',
    'count' => 1,
];
$uniqueBy = ['name'];
$update = ['count' => DB::raw('count+1')];

Model::upsert($values, $uniqueBy, $update);

If the model exists count will be incremented, if it is inserted count will equal 1. This is done on the DB level, so only one query involved.

Read more about upserts: https://laravel.com/docs/8.x/eloquent#upserts

6
  • 1
    Upsert had another purpose you can use update for this and you'll be fine. Aug 10, 2021 at 12:15
  • Update works for one model, the above works for multiple model values and/or a single one and increment count for each. I see this can be useful and accomplishes everything in 1 query as well.
    – madsem
    Aug 16, 2021 at 9:35
  • No update can also work for multiple rows of the table you're referring to the update method on the model not the update method of the query builder. Aug 16, 2021 at 11:25
  • Yes, but not with different values? So I still think this can be valuable depending on use case. I for example posted this after I had exactly this case, update many with different values and set visit count to +1
    – madsem
    Aug 19, 2021 at 9:04
  • Sure of course I'm not implying that the upsert method is useless, but in the code that you provided above it could be changed with update and nothing will go wrong. All I'm saying is that it is unnecessary in this specific context and might mislead the future viewers. Aug 19, 2021 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.