如何在laravel

时间:2015-08-20 05:54:04

标签: php laravel laravel-4 eloquent

我有一张表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,所以iam编辑我的出席现状变得缺席。

所以想要将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值是旧值。

2 个答案:

答案 0 :(得分:5)

递增()递减()不会返回查询生成器对象,因此您无法像在代码:

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

答案 1 :(得分:1)

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