How to increment database field values by 1 in Lar

2019-03-06 19:17发布

I have a table student attendance.

Fields/data sample

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

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

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

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

My controller

$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,
            ));
    }
}

标签: laravel-4
1条回答
不美不萌又怎样
2楼-- · 2019-03-06 19:21

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();
 }
}
查看更多
登录 后发表回答