maf*_*tis 9 html javascript php laravel eloquent
我试图通过javascript从第二个表中获取特定列并将其数据返回到我的视图,但我不确定它是如何完成的.
price专栏product_id,min,max和amount列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)
product
ID我的路线还是......?我正在尝试对我的代码进行一些更改,但我无法做对 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)
这是我的数据库看起来怎么样和结果之间的量2来6我得到的7000,而我得5000从2到5.
从数字7到9我根本没有结果.
从数字10到数字,我得到的是7000
任何的想法?
在后端控制器中使用相反的条件:
$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)
| 归档时间: |
|
| 查看次数: |
2348 次 |
| 最近记录: |