title: Get info by javascript in laravel
body: <p>I'm trying to get specific column from second table by javascript and return data of it to my view but i'm not sure how it can be done.</p>
  
  <h1>Logic</h1>
  
  <ol>
  <li>Products table has <code>price</code> column</li>
  <li>Discount table has <code>product_id</code>, <code>min</code>, <code>max</code> and <code>amount</code> columns</li>
  <li>I input number as quantity, if have product id in my discount table
  base on <code>min</code> and <code>max</code> return the <code>amount</code> as new price</li>
  </ol>
  
  <h1>Code</h1>
  
  <p>so far this is my codes (I am aware that specially my controller method has identifying issue to find te right data)</p>
  
  <p><code>JavaScript</code></p>
  
  <pre><code>&lt;script&gt;
    $(document).ready(function() {
      // if quantity is not selected (default 1)
      $('#newprice').empty();
      var quantity =  parseInt(document.getElementById("quantity").value);
      var shipingcost = parseFloat(quantity);
      var shipingcostnumber = shipingcost;
      var nf = new Intl.NumberFormat('en-US', {
          maximumFractionDigits:0, 
          minimumFractionDigits:0
      });
      $('#newprice').append('Rp '+nf.format(shipingcostnumber)+'');
  
      // if quantity is changed
      $('#quantity').on('change', function() {
        var quantity = parseInt(document.getElementById("quantity").value);
        var qtyminmax = $(this).val();
        if(qtyminmax) {
          $.ajax({
            url: '{{ url('admin/qtydisc') }}/'+encodeURI(qtyminmax),
            type: "GET",
            dataType: "json",
            success:function(data) {
              $('#totalPriceInTotal').empty();
              var shipingcost = parseFloat(data)+parseFloat(quantity);
              var shipingcostnumber = shipingcost;
              var nf = new Intl.NumberFormat('en-US', {
                  maximumFractionDigits:0, 
                  minimumFractionDigits:0
              });
              $('#totalPriceInTotal').append('Rp '+nf.format(shipingcostnumber)+'');
            }
          });
        }else{
          //when quantity backs to default (1)
          $('#newprice').empty();
          var quantity = parseInt(document.getElementById("quantity").value);
          var shipingcost = parseFloat(quantity);
          var shipingcostnumber = shipingcost;
          var nf = new Intl.NumberFormat('en-US', {
              maximumFractionDigits:0, 
              minimumFractionDigits:0
          });
          $('#newprice').append('Rp '+nf.format(shipingcostnumber)+'');
        }
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p><code>Route</code></p>
  
  <pre><code>Route::get('qtydisc/{id}', 'ProductController@qtydisc');
  </code></pre>
  
  <p><code>Controller</code></p>
  
  <pre><code>public function qtydisc($id){
        return response()-&gt;json(QtyDiscount::where('min', '&gt;=', $id)-&gt;orWhere('max', '&lt;=', $id)-&gt;pluck('min'));
      }
  </code></pre>
  
  <h1>Question</h1>
  
  <ol>
  <li>What should I change in my controller method to get the right data?</li>
  <li>What should I change in my JavaScript code? should I add <code>product
  ID</code> in my route as well or...?</li>
  </ol>
  
  <h1>UPDATE</h1>
  
  <p>I'm trying some changes in my code but I can't get right <code>amount</code></p>
  
  <p><code>controller</code></p>
  
  <pre><code>public function qtydisc($id, $qty){
        $price = DB::table('qty_discounts')
                -&gt;where('product_id', $id)
                -&gt;where([
                    ['min', '&gt;=', $qty],
                    ['max', '&lt;=', $qty],
                ])
                // -&gt;where('min', '&gt;=', $qty)
                // -&gt;where('max', '&lt;=', $qty)
                -&gt;select('amount')
                -&gt;first();
        return response()-&gt;json($price);
      }
  </code></pre>
  
  <p><code>route</code></p>
  
  <pre><code>Route::get('qtydisc/{id}/{qty}', 'ProductController@qtydisc');
  </code></pre>
  
  <p><code>javascript</code></p>
  
  <pre><code>//as before...
  
  $('#quantity').on('change', function() {
        var idofproduct = ["{{$product-&gt;id}}"]; //added
        var quantity = parseInt(document.getElementById("quantity").value);
        var qtyminmax = $(this).val();
        if(qtyminmax) {
          $.ajax({
            url: '{{ url('admin/qtydisc') }}/'+idofproduct+'/'+encodeURI(qtyminmax), //changed
            type: "GET",
            dataType: "json",
            success:function(data) {
              $('#totalPriceInTotal').empty();
              var shipingcost = parseFloat(data)+parseFloat(quantity);
              var shipingcostnumber = shipingcost;
              var nf = new Intl.NumberFormat('en-US', {
                  maximumFractionDigits:0, 
                  minimumFractionDigits:0
              });
              $('#totalPriceInTotal').append('Rp '+nf.format(shipingcostnumber)+'');
            }
          });
        }else{
  //rest of it as before
  </code></pre>
  
  <h2>Screenshot</h2>
  
  <p><a href="https://i.stack.imgur.com/qMaWL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qMaWL.png" alt="screenshotdb"></a></p>
  
  <p>that's how my database look like and as results for quantity between <code>2</code> to <code>6</code> i get <code>7000</code> while i have to get <code>5000</code> from <code>2</code> to <code>5</code>.</p>
  
  <p>From number <code>7</code> to <code>9</code> i get no results at all.</p>
  
  <p>From number <code>10</code> to up all i get is <code>7000</code></p>
  
  <p>Any idea?</p>