【问题标题】:how to use increment() and decrement() in laravel如何在 laravel 中使用 increment() 和 decrement()
【发布时间】:2015-08-20 05:54:04
【问题描述】:

我有一张桌子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 的每个学生递增和递减。

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

例如: 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 和(当前 == 1) studid = 30 total_p = 6 和 total_a = 1 和其他 studid 值是旧值。

【问题讨论】:

  • 你确定你的SQL逻辑正确吗?
  • 对不起,我不知道......但我想要这样......如果(present==1)然后增加(stotal_p,1)和减少(stotal_a,1)和if(present== 0) 然后在条件为真的情况下递增 (stotal_a,1) 和递减 (stotal_p,1)。
  • 将您的构建器代码转换为 MySQL 查询并针对出勤表运行。
  • 如果你不介意..我的逻辑是错误的......你能告诉我什么是正确的代码吗??
  • 先生,我不想将我的构建器代码转换为 mysql....如何在查询构建器中使用增量()和减量()??

标签: php laravel laravel-4 eloquent


【解决方案1】:

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');

【讨论】:

  • 先生,这段代码工作正常..但是当我编辑一个学生时出现一个问题(例如:studid =35),我只想执行这个代码 studid(35) raw,而不是所有其他学生... 像 update 函数一样具有递增和递减。使用此代码更改所有 studid 值。
  • 在 studid 上存在 where() 约束,因此查询无法更新其他 studid。粘贴更多代码,我们会找到原因
  • 做“dd($student->id, $query->toSql());”并粘贴结果
  • 先生,我收到一条错误消息。在非对象上调用成员函数 toSql()
  • 在答案中的查询上执行此操作,而不是在代码中的查询中
【解决方案2】:

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

Laravel 文档和示例

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);

获取学生出勤率

$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');

【讨论】:

猜你喜欢
  • 2021-01-14
  • 2020-01-07
  • 1970-01-01
  • 1970-01-01
  • 2013-07-23
  • 2013-02-18
  • 2016-02-07
  • 1970-01-01
相关资源
最近更新 更多