M
M
Muhammad2015-07-04 18:17:37
MySQL
Muhammad, 2015-07-04 18:17:37

Reduce quantity per request?

There is an array of identifiers and an array of numbers. It is necessary for each record with id from the first array to reduce the value of the count field in the table by the value from the second. i.e.:
$ids = [1, 2, 3];
$values ​​= [5, 7, 10];
The count value of the entry with id 1 is decremented by 5, and so on. Can you please tell me how to implement this in one request in Eloquent, if at all possible

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2015-07-04
@xmoonlight

Can be done in pure mysql.
(see last paragraph)
("Updating a record with a conditional selection in one query")

V
Vyacheslav Plisko, 2015-07-04
@AmdY

This issue has already been discussed, because. you have different things, then only the option with CASE in mysql is possible, which is better not to do, you get nonsense like
Foo::whereIn($array1)->update('counter', Db::raw('counter - (CASE WHEN 1 THEN 5.......)'));
Don't skimp on matches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question