mercredi 18 mai 2016

What kind of error which will be catch by Laravel transaction?

I'm really concern on my transaction with Database and my code structure. I have create more method to insert, update, delete data to database but sometime some of them will be break or miss that why I try to used with Laravel transaction and PHP catch and throw exception to rollback and avoid any errors happy during those method process they transaction.

By they way I have not much understand yet how Try Catch and Laravel Transaction catch those errors and return or throw to the end user.

However I've tried to find all kind of errors will happen and how can I throw those error type to end users on catch block.

This function work find but I want to check if any errors happen and how can I return those type of errors to end user by using Laravel Transaction and Try.

 public function Dosomething($id)
    {
     if (Request::ajax()) {    
        $data = [
          'till_account_id' => Request::input('till_account_id'),
          'from_account' => Request::input('from_account'),
          'to_account' => Request::input('to_account'),
          'till_user_id' => Request::input('till_user_id'),
          'branch_id' => Request::input('branch_id'),
          'operate_by' => Request::input('operate_by'),
          'type' => Request::input('type'),
         'tranx_time' => date('Y-m-d H:m:s', time())
       ];
      DB::beginTransaction();
     try {
         $update = Teller::where('id', '=', $id)->update(['balance' => Request::input('balanceCommision')]);
         if (!empty($update)) {
             $data['cash_out'] = Request::input('cash_out');
             $trans_cashIn = DB::table('till_transaction')->insertGetId($data);

             $data['cash_in'] = Request::input('cash_in');
             $data['cash_out'] = 0;
             $trans_cashOut = DB::table('till_transaction')->insertGetId($data);
        }
            $test = DB::commit();
            return ['up' => $update, 'insCashIn' => $trans_cashIn, 'insCashOut' => $trans_cashOut, 'data' => $data, 'cash_out' => $data['cash_out'],'commit'=>$test];
       } catch (Exception $e) {
           DB::rollBack();
           throw $e;
      }
   }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1YBzgCH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire