php 如何在 Laravel 中使用 increment() 和 decrement()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32110502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use increment() and decrement() in laravel
提问by cnbwd
I have a table total_count
我有一张桌子 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 |
+----+--------+-------+------+---------+---------+---------+
I want to increment and decrement for each student total_p
and total_a
.
我想递增和递减为每个学生total_p
和total_a
。
when i am edit my student attendance list.
当我编辑我的学生出勤表时。
eg:studid 30 total_p = 5 and total_a= 2 ,so iam edit my attendance present become absent .
例如:studid 30 total_p = 5 和 total_a= 2 ,所以我编辑我的出席现在变得缺席。
so want decrement total_p by 1 and increment total_a by 1.
所以想要将 total_p 减少 1 并将 total_a 增加 1。
So I'd like to get the total of each month for each studid
and a increment and decrement of total_p
and total_a
for the total months.
所以我想以获得每个总每个月的studid
和的增量和减量 total_p
和total_a
总个月。
My controllercode is
我的控制器代码是
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'),
]);
}
}
but it doesn't work..
但它不起作用..
How to use increment()
and decrement()
in query builder format?
如何使用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.
例如:如果我只编辑studid = 30 出勤增量total_p 值1 和(present == 1)studid = 30 total_p = 6 和total_a = 1 并且其他studid 值是旧值。
回答by jedrzej.kurylo
increment()and decrement()do not return a Query Builderobject, so you cannot chain your calls like you do in your code:
increment()和decrement()不返回查询生成器对象,因此您不能像在代码中那样链接调用:
->increment('stotal_p', 1)->decrement('stotal_a', 1);
You'll need to call each method separately. Moreover, 1is the default value for the increment/decrement, so no need to pass it.
您需要分别调用每个方法。此外,1是increment/decrement的默认值,因此无需传递它。
This should do the trick:
这应该可以解决问题:
$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');
回答by Aamer Shahzad
From Laravel 5.7+ you can increment or decrement the given column by using increment()
or decrement()
methods without writing the manual update statement.
从 Laravel 5.7+ 开始,您可以使用increment()
或decrement()
方法增加或减少给定的列,而无需编写手动更新语句。
Laravel Doc & Example
Laravel 文档和示例
https://laravel.com/docs/5.8/queries#increment-and-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);
Get the Student Attendance
获取学生出勤率
$studentAtd = DB::table($wys_total_attend_table)
->where('studid',$student->id)
->where('smonth','=',$date_exploded[1])
->where('syear','=',$date_exploded[2]);
Increment or Decrement Attendance column
增加或减少出勤栏
$studentAtd->increment('stotal_a');
$studentAtd-> decrement('stotal_p');