gpt4 book ai didi

laravel-4 - 如何在 Laravel 中将数据库字段值递增 1

转载 作者:行者123 更新时间:2023-11-30 22:37:27 25 4
gpt4 key购买 nike

我有一张学生出勤表。

字段/数据示例

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。

如果我输入两个学生 present = 1 所以我想要 total_p value 20=3, 21=2, 22=3

如何获取数据库字段值并加1?

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

最佳答案

我想你只是想更新 total_p 和 total_a 列的每条记录只是为了简单:

//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-4 - 如何在 Laravel 中将数据库字段值递增 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32084724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com