如何在Laravel中将数据库字段值增加1 [英] How to increment database field values by 1 in Laravel

查看:3713
本文介绍了如何在Laravel中将数据库字段值增加1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的餐桌学生出勤率.

字段/数据示例

id | studid | cls_id | smonth | syear | total_p | total_a
1  |   20   |   2    |   08   | 2015  |   2     |   1
2  |   21   |   2    |   08   | 2015  |   1     |   0
3  |   22   |   2    |   08   | 2015  |   2     |   1

我想检查最后一次更新中每个学生的total_ptotal_a值是多少,然后递增1.

I want, to check what is the total_p and total_a value of each students in last update and then increment 1.

如果我输入的两个学生都是present = 1,那么我要total_p value 20=3, 21=2, 22=3

If I am enter the both students are present = 1 so I want total_p value 20=3, 21=2, 22=3

如何获取数据库字段值并递增1.?

How to get database field values and increment 1's.?

我的控制器

$present = Input::get($student->id);
if ($checkatt)
{
    if ($present == 1)
    {
        DB::table($wys_total_attend_table)->where('studid', $student->id)
            ->where('smonth', $date_exploded[1])
            ->where('syear', $date_exploded[2])
            ->where('stotal_p', 1)
            ->update(array(
                'stotal_p' => 1 + 1,
            ));
        DB::table($wys_total_attend_table)->where('studid', $student->id)
            ->where('smonth', $date_exploded[1])
            ->where('syear', $date_exploded[2])
            ->where('stotal_p', 0)
            ->update(array(
                'stotal_p' => 1,
                'stotal_a' => 0,
            ));
    } elseif ($present == 0)
    {
        DB::table($wys_total_attend_table)->where('studid', $student->id)
            ->where('smonth', $date_exploded[1])
            ->where('syear', $date_exploded[2])
            ->where('stotal_a', 1)
            ->update(array(
                'stotal_a' => 1 + 1,
            ));
        DB::table($wys_total_attend_table)->where('studid', $student->id)
            ->where('smonth', $date_exploded[1])
            ->where('syear', $date_exploded[2])
            ->where('stotal_a', 0)
            ->update(array(
                'stotal_a' => 1,
            ));
        DB::table($wys_total_attend_table)->where('studid', $student->id)
            ->where('smonth', $date_exploded[1])
            ->where('syear', $date_exploded[2])
            ->where('stotal_p', 1)
            ->where('stotal_a', 0)
            ->update(array(
                'stotal_a' => 0 + 1,
            ));
    }
}

推荐答案

我认为您只是想更新total_p和total_a列的每条记录,只需使其简单即可:

I think u just want to update each record of total_p and total_a column just make it simple:

//get the id of student
$student_id = Input::get('student_id');
$present = Input::get('status'); //dropdown value 0,1
//You need a model for your table let say:
#Student.php
<?php
class Student extends Eloquent{
    protected $table = 'students'; //table name 
}
//Your Controller codes
public function findStudent($id, $status){
$query=Student::find($id);
if($query->count() && $status==1 ){ //status 1 = present
  $query->total_p += 1; //plus one value in the total_p column in the tbl.
  $query->save();
 }else{
  $query->total_a +=1;
  $query->save();
 }
}

这篇关于如何在Laravel中将数据库字段值增加1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆