title: Optimise AJAX - Laravel function
body: <p>I'm struggling a bit with some formulas, the idea is to have just one formula/function for everything, so it'll be easy to maintain and will be robust.</p>
  
  <p>The problem is trying to combine AJAX calls and laravel functions.</p>
  
  <p>From one side I have a AJAX Datatables controller (the calls need to be in this format):</p>
  
  <pre><code>public function userData(Request $request)
      {
          $event = User::select(
              'users.*',
              DB::raw('IFNULL(b.balance,0) as balance'),
          )
          -&gt;leftJoin(DB::raw('(SELECT seller_id, SUM(total) as balance FROM transactions WHERE concept IN ("TPV") AND status = "ok" GROUP by buyer_id)as b'), 'b.seller_id', '=', 'users.id')
          -&gt;get();
          return $this-&gt;formatView($request, $event, 'user'); 
      }
  </code></pre>
  
  <p>Then, the formula I use for the rest of the web is in a Function inside a Model:</p>
  
  <pre><code>public function Balance($seller_id = false){
          return Transaction::emitted()
              -&gt;where('event_id', $this-&gt;id)
              -&gt;where('seller_id', $this-&gt;seller_id)
              -&gt;whereIn('concept', ['TPV'])
              -&gt;where('status', 'ok')
              -&gt;sum('total'); 
      }
  </code></pre>
  
  <p>The question is: Do you have an idea of how to use just one formula/function for everything?</p>