如何在laravel中使用increment()和递减()



我有一个表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,所以我编辑我的出席变成缺席。

所以要将total_p减1,将total_a增1

因此,我想获得每个studid的每个月的总数,以及total_ptotal_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()在查询生成器格式?

例如:如果我只编辑studid = 30出席增加total_p值1和(present == 1) studid = 30 total_p = 6和total_a = 1和其他研究值为旧值。

increment() dec递减()不返回Query Builder对象,因此您不能像在代码中那样链接调用:

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

你需要分别调用每个方法。此外,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');

从Laravel 5.7+开始,您可以使用increment()decrement()方法增加或减少给定的列,而无需编写手动更新语句。

Laravel Doc &示例

https://laravel.com/docs/5.8/queries递增和递减

DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);

获取学生出勤率

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

增加或减少考勤列

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

最新更新