1.使用数据库事务的时候需要传递参数,使用了use关键字,否则的话传递不进去,代码如下:
public function postVote(Request $request){ $data = $request->all(); $count = DB::table("vote")->where("workdid",$data[‘id‘])->where("ip", $data[‘ip‘])->count(); if(DB::table("vote")->where("workdid",$data[‘id‘])->where("ip", $data[‘ip‘])->count()>0){ $ret = [‘state‘ => ‘fail‘, ‘message‘ => ‘您已经给这个作品投过一票!‘]; }else{ DB::transaction(function () use($data){ DB::table("work")->where("id", $data["id"])->increment("vote"); DB::table("vote")->insert(["workdid"=>$data["id"], "ip"=>$data[‘ip‘]]); }); $ret = [‘state‘ => ‘success‘, ‘message‘ => ‘投票成功‘]; } return response()->json($ret); }
时间: 2024-10-12 04:04:18