【问题标题】:Laravel Datatables ajax unable to send a parameter to queryLaravel Datatables ajax无法发送参数进行查询
【发布时间】:2019-07-17 03:24:23
【问题描述】:

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

我收到"Undefined index: month"

这是我的代码 sn-ps:

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);
}

【问题讨论】:

  • 我无法从您提供的代码中判断 $brandData 的设置位置,但如果它应该包含数组或其他内容中的所有参数,您可以: 在页面上,看看它给了你什么?然后你就可以弄清楚如何正确获取索引了。

标签: mysql ajax laravel datatables


【解决方案1】:

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

url('brand_ajax/' . $month)

【讨论】:

    猜你喜欢
    • 2016-07-03
    • 2015-10-26
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多