doubo1871 2016-04-29 03:56
浏览 30

如果我在控制器方法中创建方法而不是使用eloquent或查询构建器,Laravel5事务是否有效

I'm so concern about my operation to used Mysql statement like update, delete, insert and within Laravel Eloquent or Query builder because I've create more Mysql statement with more conditional inside of my method in Controller so if one of that conditional or something occurs or meet any problem during I'm processing my method in controller Method I will lost my information or data my data which I want to insert or update to my database.

As below function I used setNotification() which I create in Notification Model and I have method inside that Model which I want to call it to post_data is a method in Controller so if I do so does DB::beginTransaction() will work or not because I prefer to keep all Mysql statement inside all method in Model.

now I currently using Laravel Transaction with Try catch

public function post_data() {

  if (Request::ajax() && $this->CheckPermId_from_session(90)) {

     $res = null;
     $data = Request::except(['_token']);
     $rules = [//rules varray];
     $data = [//Input data];
     $data['tranx_time'] = date("y-m-d H:m:s", time());
     $val = Validator::make($data, $rules);
     if ($val->fails()) {

           $res = ['res' => false, 'form' => false, 'data', $data];
     } else {

           DB::beginTransaction();
           try {

          //$update = Teller::where('id', '=', Request::input('teller_till_id'))->update(array('balance' => Request::input('tell_balance')));
           $updateTeller = Teller::where('id', '=', Request::input('chief_till_id'))->update(array('balance' => Request::input('last_chief_balance')));
          $insertId = DB::table('till_transaction')->insertGetId($data);
    if ($insertId && $updateTeller) {
          $this->notification->setNotification([$data['to_account'], json_encode($this->group_code), $insertId, Request::input('chief_till_id'), $data['type'], date("Y-m-d H:m:s", time()), $data['type']]);
          $res = ['res' => true, 'data', $data];
    } else {
               $res = ['res' => false, 'data', $data];
           }
           DB::commit();
       } catch (Exception $e) {
           DB::rollBack();
           throw $e;
      }
    }
    return $res;
   }
  }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单