title: Dynamic Sum of Fields MySQL
body: <p>I've table materials which has relation with table cost prices which has multiple records quantities.</p>
  
  <p>Now i have another table which is receipts when i make a receipt it shortens the sum of quantities by quantity of receipt.</p>
  
  <p>is there anyway in <code>MySQL</code> to add <strong>dynamic</strong> field which calculates the sum quantities and keeps eye on change like when i sell (make receipt) it's dynamically shortens the sum of quantities ?
  this is the schema in Laravel     </p>
  
  <p>//receipts </p>
  
  <pre><code>     Schema::create('receipts', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;unsignedBigInteger('client_id');
          $table-&gt;unsignedBigInteger('material_id');
          $table-&gt;double('payroll',8,2);
          $table-&gt;integer('quantity');
          $table-&gt;integer('type');
          $table-&gt;timestamps();
  
        });
  
        Schema::create('cost_prices', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;unsignedBigInteger('material_id');
          $table-&gt;unsignedBigInteger('supplier_id');
          $table-&gt;integer('cost_price');
          $table-&gt;integer('quantity');
          $table-&gt;timestamps();
      });
  </code></pre>