A
A
Andre972022-02-16 15:03:39
Laravel
Andre97, 2022-02-16 15:03:39

How to update the collection of models?

Good afternoon. There is a collection of models users. It is necessary to check the field in each model spent, and if it is more than 1000, add 1 to it and overwrite this model. I can do it through foreach, but I need it to be beautiful using laravel...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pLavrenov, 2022-02-16
@Andre97

User::where('spent', '>', 1000)->increment('spent');

->increment() - Increase
->decrement() - Decrease The
second parameter can be any number to be added or subtracted.

V
vfreelance, 2022-02-16
@vfreelancer

->where('spent', '>', 1000)
->update([
'spent'' => \DB::raw('spent+1')
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question