通过javascript在laravel中获取信息

maf*_*tis 9 html javascript php laravel eloquent

我试图通过javascript从第二个表中获取特定列并将其数据返回到我的视图,但我不确定它是如何完成的.

逻辑

  1. 产品表有price专栏
  2. 折扣表有product_id,min,maxamount
  3. 我输入的数字作为数量,如果有产品ID在我的折扣表的基础min,并max返回amount新价格

到目前为止这是我的代码(我知道特别是我的控制器方法有识别问题,找到正确的数据)

JavaScript

<script>
  $(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)+'');
      }
    });
  });
</script>
Run Code Online (Sandbox Code Playgroud)

Route

Route::get('qtydisc/{id}', 'ProductController@qtydisc');
Run Code Online (Sandbox Code Playgroud)

Controller

public function qtydisc($id){
      return response()->json(QtyDiscount::where('min', '>=', $id)->orWhere('max', '<=', $id)->pluck('min'));
    }
Run Code Online (Sandbox Code Playgroud)

  1. 我应该在控制器方法中更改什么才能获得正确的数据?
  2. 我的JavaScript代码应该更改什么?我应该添加product ID我的路线还是......?

UPDATE

我正在尝试对我的代码进行一些更改,但我无法做对 amount

controller

public function qtydisc($id, $qty){
      $price = DB::table('qty_discounts')
              ->where('product_id', $id)
              ->where([
                  ['min', '>=', $qty],
                  ['max', '<=', $qty],
              ])
              // ->where('min', '>=', $qty)
              // ->where('max', '<=', $qty)
              ->select('amount')
              ->first();
      return response()->json($price);
    }
Run Code Online (Sandbox Code Playgroud)

route

Route::get('qtydisc/{id}/{qty}', 'ProductController@qtydisc');
Run Code Online (Sandbox Code Playgroud)

javascript

//as before...

$('#quantity').on('change', function() {
      var idofproduct = ["{{$product->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
Run Code Online (Sandbox Code Playgroud)

截图

screenshotdb

这是我的数据库看起来怎么样和结果之间的量26我得到的7000,而我得500025.

从数字79我根本没有结果.

从数字10到数字,我得到的是7000

任何的想法?

F.I*_*gor 0

在后端控制器中使用相反的条件:

$price = DB::table('qty_discounts')
          ->where('product_id', $id)
          ->where('min', '<=', $qty)
          ->where('max', '>=', $qty)
          ->select('amount')
          ->first(); 
Run Code Online (Sandbox Code Playgroud)

所以对于$id= 2 和$qty= 5 你会得到

product id = 2  and `min` <= 5 and `max` >= 5
Run Code Online (Sandbox Code Playgroud)

选择第一行 ( row_id= 1)