作者:繁华落尽的星空 | 来源:互联网 | 2022-12-09 11:55
我试图通过Javascript从第二个表中获取特定列并将其数据返回到我的视图,但我不确定它是如何完成的.
逻辑
产品表有price
专栏
折扣表有product_id
,min
,max
和amount
列
我输入的数字作为数量,如果有产品ID在我的折扣表的基础min
,并max
返回amount
新价格
码
到目前为止这是我的代码(我知道特别是我的控制器方法有识别问题,找到正确的数据)
Javascript
Route
Route::get('qtydisc/{id}', 'ProductController@qtydisc');
Controller
public function qtydisc($id){
return response()->json(QtyDiscount::where('min', '>=', $id)->orWhere('max', '<=', $id)->pluck('min'));
}
题
我应该在控制器方法中更改什么才能获得正确的数据?
我的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);
}
route
Route::get('qtydisc/{id}/{qty}', 'ProductController@qtydisc');
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
截图
这是我的数据库看起来怎么样和结果之间的量2
来6
我得到的7000
,而我得5000
从2
到5
.
从数字7
到9
我根本没有结果.
从数字10
到数字,我得到的是7000
任何的想法?