laravel mysql添加字段,如何在Laravel中将数据库字段值增加1

在Laravel中,要检查并递增学生出席记录的total_p和total_a字段,可以通过Eloquent ORM简化操作。根据出席状态(1表示出席,0表示缺席),使用Student模型找到相应学生,然后直接更新对应字段的值,例如`$query->total_p += 1`或`$query->total_a += 1`,最后保存更改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,

));

}

}

解决方案

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

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值