我在Laravel 5.0上花了更多的时间用于数据库事务,但是当我改为Laravel版本5.3。*它不能正常工作,即使我已经禁用了commit()方法,所有数据仍然继续插入到数据库中。
if ($request->isMethod('post')) {
DB::beginTransaction();
$cats = new Cat();
$cats->parent_id = $this->request->input('category_id');
$cats->status = ($this->request->input('status'))?$this->request->input('status'):0;
if($res['cat'] = $cats->save()) {
$catD = new CategoryDescriptions();
$catD->category_id = $cats->id;
$catD->language_id = 1;
$catD->name = $this->request->input('en_name');
if($res['cat2'] = $catD->save()){
$catD2 = new CategoryDescriptions();
$catD2->category_id = $cats->id;
$catD2->language_id = 2;
$catD2->name = $this->request->input('kh_name');
$res['all'] = $catD2->save();
}
}
if($res){
//DB::commit();
return $res;
}
return [false];
}
答案 0 :(得分:0)
选中此行($res['cat']
是boolean
):
if($res['cat'] = $cats->save()) {
这个($res
是array
。你将数组比作布尔值!):
if($res){
正确的条件应为:
$res = array(); // the first line of your method
// .... your http method check, start transaction, etc.
if (!in_array(false, $res, true)) { // check if array doesn't contain 'false values'
DB::commit();
}