duanlangwen9597
2017-07-21 07:39
浏览 473
已采纳

如何在laravel中使用db transaction?

I try this :

public function destroy($id)
{
    DB::beginTransaction();
    try {
        $product = $this->product_repository->find($id);
        $result = $product->categories()->detach();
        if($result) {
            list($status,$instance) = $this->product_repository->delete($id);
        }
        DB::commit();
        return ['status'=>true,'data'=>$status];
    } catch (\Exception $e) {
        DB::rollback();
        return ['status'=>false, 'message'=>$e->getMessage()];
    }
}

If the code executed, $this->product_repository->delete($id) not work / not success delete.

But this : $product->categories()->detach();, it works / success deleted.

How to if delete product failed, delete category also failed?

图片转代码服务由CSDN问答提供 功能建议

我试试这个:

  public function destroy($ id  )
 {
 DB :: beginTransaction(); 
尝试{
 $ product = $ this-> product_repository-> find($ id); 
 $ result = $ product-> categories()  - > detach(); 
 if($ result){
 list($ status,$ instance)= $ this-> product_repository-> delete($ id); 
} 
 DB :: commit  (); 
 return ['status'=> true,'data'=> $ status]; 
} catch(\ Exception $ e){
 DB :: rollback(); 
 return ['  status'=> false,'message'=> $ e-> getMessage()]; 
} 
} 
   
 
 

如果代码已执行 , $ this-> product_repository-> delete($ id) not working / not success delete。

但是: $ product-> categories() - > detach(); ,它的工作/成功删除。 \ n

如果删除产品失败,删除类别也失败了?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpao1926 2017-07-21 07:42
    已采纳

    You can't add return statement inside transaction that halts entire process and DB::rollback() is executed.

    To switch the return, You can define a boolean variable and make false while you catch exception.

    Like this:

    public function destroy($id)
    {
    
        $success = true;
    
        DB::beginTransaction();
    
        try{
    
            // Your Code
    
            $product = $this->product_repository->find($id);
            $result = $product->categories()->detach();
    
            if($result) {
                list($status,$instance) = $this->product_repository->delete($id);
            }
    
            DB::commit();
    
        }catch(\Exception $e){
    
            DB::rollback();
    
            $success = false;
    
        }
    
        if($success){
            // Return data for successful delete
        }
    
        else{
            // Return data for unsuccessful delete
        }
    }
    

    Hope you understand.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongtao5104 2017-07-21 07:44

    You can use it like this:

    $returnResult = [];
    
    DB::beginTransaction();
    
    try {
    
        ...
    
        DB::commit();
    
        $returnResult['status'] = true;
        $returnResult['data'] = $status;
    } catch (...) {
    
        ...
    
        DB::rollback();
    
        $returnResult['status'] = true;
        $returnResult['message'] = $e->getMessage();
    }
    
    return $returnResult;
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题