I have a table 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
.
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 .
so want decrement total_p by 1 and increment total_a by 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.
My controller code 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?
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.
increment() and decrement() do not return a Query Builder object, so you cannot chain your calls like you do in your code:
->increment('stotal_p', 1)->decrement('stotal_a', 1);
You'll need to call each method separately. Moreover, 1 is the default value for the increment/decrement, so no need to pass it.
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');
From Laravel 5.7+ you can increment or decrement the given column by using increment()
or decrement()
methods without writing the manual update statement.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With