gpt4 book ai didi

mysql - Laravel Datatables ajax无法发送参数进行查询

转载 作者:行者123 更新时间:2023-11-29 16:15:31 25 4
gpt4 key购买 nike

我正在使用 Laravel 数据表。我成功地通过查询从 MySQL 数据库加载数据。我遇到的问题是能够通过发送月份编号来按月查询表。我无法成功将包含月份号的参数发送到后端的 ajax 函数。我尝试了很多变体,但没有任何效果。

我得到“未定义索引:月份”

这是我的代码片段:

Javascript ajax:

<script>
$(function() {
var month = "<?= $brandData['month'] ?>"; // contains a month number
$('#accounts').DataTable({
processing: true,
serverSide: true,
type: "POST",
ajax: '{!! url('brand_ajax') !!}',
data : {'month' : month },
columns: [
{ data: 'brand_name', name: 'brand_name' , className: 'text-xl-left'},
{ data: 'brand_volume', name: 'brand_volume', className: 'text-xl-right' },
{ data: 'brand_margin', name: 'brand_margin' , className: 'text-xl-right'},
{ data: 'brand_commission', name: 'brand_commission', className: 'text-xl-right' },
]
});
});
</script>

这是我的ajax函数返回数据

public function brand_ajax()
{
$month = $_POST["month"];
$brands = SaleInvoice::select(DB::raw('brands.name as brand_name,
avg(NULLIF(margin,0)) as brand_margin,
sum(commission) as brand_commission,
sum(amt_invoiced) as brand_volume
'))
->join('brands', 'brands.ext_id', '=', 'saleinvoices.brand_id')
->where('brands.is_active', '=', true)
// ->whereRaw('MONTH(saleinvoices.invoice_date) = ?', (11))
->whereRaw('MONTH(saleinvoices.invoice_date) = ?', ($month))
->having('brand_volume', '>', 0)
->groupBy('brands.name')
->get();


return DataTables::of($brands)
->editColumn('brand_commission', function ($brand) {
return number_format($brand->brand_commission, 2);
})
->editColumn('brand_margin', function ($brand) {
return number_format($brand->brand_margin, 2);
})
->editColumn('brand_volume', function ($brand) {
return number_format($brand->brand_volume, 2);
})
->make(true);
}

最佳答案

我自己解决了。我刚刚将参数添加到 URL 字符串中。

url('brand_ajax/' . $month)

关于mysql - Laravel Datatables ajax无法发送参数进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54837148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com