cnb*_*bwd 4 php laravel eloquent laravel-4
我有桌子 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 |
+----+--------+-------+------+---------+---------+---------+
Run Code Online (Sandbox Code Playgroud)
我想递增和递减为每个学生total_p和total_a。
当iam编辑我的学生出勤清单时。
例如:研究了30 total_p = 5和total_a = 2,所以我无法编辑我的出席情况。
因此要将total_p减1,并将total_a减1。
所以我想以获得每个总每个月的studid和的增量和减量 total_p和total_a总个月。
我的控制器代码是
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()查询生成器格式?
例如:如果iam仅编辑学习= 30出勤增量total_p值1和(present == 1)学习= 30 total_p = 6和total_a = 1且其他学习值是旧值。
increment()和decrement()不会返回查询生成器对象,因此您不能像在代码中那样链接调用:
->increment('stotal_p', 1)->decrement('stotal_a', 1);
Run Code Online (Sandbox Code Playgroud)
您需要分别调用每个方法。此外,1是递增/递减的默认值,因此无需传递它。
这应该可以解决问题:
$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');
Run Code Online (Sandbox Code Playgroud)
从 Laravel 5.7+ 开始,您可以使用increment()或decrement()方法增加或减少给定的列,而无需编写手动更新语句。
https://laravel.com/docs/5.8/queries#increment-and-decrement
DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);
Run Code Online (Sandbox Code Playgroud)
$studentAtd = DB::table($wys_total_attend_table)
->where('studid',$student->id)
->where('smonth','=',$date_exploded[1])
->where('syear','=',$date_exploded[2]);
Run Code Online (Sandbox Code Playgroud)
$studentAtd->increment('stotal_a');
$studentAtd-> decrement('stotal_p');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20510 次 |
| 最近记录: |