gpt4 book ai didi

php - 如何在 laravel 中使用 increment() 和 decrement()

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:08:07 28 4
gpt4 key购买 nike

我有一张表 total_count

+----+--------+-------+------+---------+---------+---------+
| id | studid | month | year | acls_id | total_p | total_a |
+----+--------+-------+------+---------+---------+---------+
| 1 | 30 | 08 | 2015 | 12 | 5 | 2 |
| 2 | 35 | 08 | 2015 | 12 | 5 | 2 |
| 3 | 52 | 08 | 2015 | 12 | 5 | 2 |
| 4 | 53 | 08 | 2015 | 12 | 5 | 2 |
| 5 | 54 | 08 | 2015 | 12 | 5 | 2 |
| 6 | 55 | 08 | 2015 | 12 | 5 | 2 |
| 7 | 30 | 09 | 2015 | 12 | 3 | 0 |
| 8 | 35 | 09 | 2015 | 12 | 3 | 0 |
| 9 | 52 | 09 | 2015 | 12 | 2 | 1 |
| 10 | 53 | 09 | 2015 | 12 | 3 | 0 |
| 11 | 54 | 09 | 2015 | 12 | 3 | 0 |
| 12 | 55 | 09 | 2015 | 12 | 3 | 0 |
+----+--------+-------+------+---------+---------+---------+

我想为每个学生 total_ptotal_a 递增和递减。

当我编辑我的学生出勤列表时。

eg: studid 30 total_p = 5 and total_a= 2 ,so iam edit my attendance present become absent .

所以要将 total_p 减 1,将 total_a 加 1。

所以我想得到每个 studid 的每个月的总数以及 total_ptotal_a 的增量和减量总月数。

我的 Controller 代码是


foreach ($student as $student) {
if ($present == 0) {
$query = DB::table($wys_total_attend_table)
->where('studid', $student->id)
->where('smonth', '=', $date_exploded[1])
->where('syear', '=', $date_exploded[2])
->update([
'stotal_p' => DB::raw('stotal_p - 1'),
'stotal_a' => DB::raw('stotal_a + 1'),
]);
} elseif ($present == 1) {
$query = DB::table($wys_total_attend_table)
->where('studid', $student->id)
->where('smonth', '=', $date_exploded[1])
->where('syear', '=', $date_exploded[2])
->update([
'stotal_p' => DB::raw('stotal_p + 1'),
'stotal_a' => DB::raw('stotal_a - 1'),
]);
}
}

但它不起作用..

如何在查询构建器格式中使用 increment()decrement()

for eg: if i only edit studid = 30 attendance increment total_p value 1 and (present == 1) studid = 30 total_p = 6 and total_a = 1 and other studid values are old value.

最佳答案

increment()decrement() 不返回 Query Builder 对象,因此您不能像在代码:

->increment('stotal_p', 1)->decrement('stotal_a', 1); 

您需要分别调用每个方法。此外,1increment/decrement 的默认值,因此无需传递它。

这应该可以解决问题:

$query = DB::table($wys_total_attend_table)
->where('studid',$student->id)
->where('smonth','=',$date_exploded[1])
->where('syear','=',$date_exploded[2]);

$query->increment('stotal_a');
$query->decrement('stotal_p');

关于php - 如何在 laravel 中使用 increment() 和 decrement(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110502/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com