title: Will laravel database transaction lock table?
body: <p>I use laravel5.5's database transaction for online payment application. I have a  company_account table to record each payment(<code>type</code>, <code>amount</code>, <code>create_at</code>, <code>gross_income</code>). I need to access the last record's <code>gross_income</code>, when a new record created. So I need to lock the table when the transaction with read and write table lock to avoid many payments at the same time.</p>
  
  <p>I've refer to laravel's doc, but I don't sure if the transaction will lock the table. If the transaction will lock the table, what's the lock type(read lock, write lock or both)?</p>
  
  <pre><code>DB::transaction(function () {
      // create company_account record
  
      // create use_account record
  }, 5);
  </code></pre>
  
  <hr>
  
  <p>Code:</p>
  
  <pre><code>DB::transaction(function ($model) use($model) {
      $model = Model::find($order-&gt;product_id);
      $user = $model-&gt;user;
  
      // **update** use_account record
      try {
          $user_account = User_account::find($user-&gt;id);
      } catch (Exception $e){
          $user_account = new User_account;
          $user_account-&gt;user_id  = $user-&gt;id;
          $user_account-&gt;earnings = 0;
          $user_account-&gt;balance  = 0;
      }
      $user_account-&gt;earnings += $order-&gt;fee * self::USER_COMMISION_RATIO;
      $user_account-&gt;balance += $order-&gt;fee * self::USER_COMMISION_RATIO;
      $user_account-&gt;save();
  
      // **create** company_account record
      $old_tiger_account = Tiger_account::latest('id')-&gt;first();
  
      $tiger_account = new Tiger_account;
      $tiger_account-&gt;type = 'model';
      $tiger_account-&gt;order_id = $order-&gt;id;
      $tiger_account-&gt;user_id = $user-&gt;id;
      $tiger_account-&gt;profit = $order-&gt;fee;
      $tiger_account-&gt;payment = 0;
      $tiger_account-&gt;gross_income = $old_tiger_account-&gt; gross_income + $order-&gt;fee;
      $tiger_account-&gt;save();
  }, 5);
  </code></pre>
  
  <p>references:<br>
      &emsp;<a href="https://stackoverflow.com/q/17084723/6521116">How to pass parameter to Laravel DB::transaction()</a></p>