我有一个Laravel应用,其中我正在使用jQuery DataTables Master / Details显示一些额外的信息。我可以将数据显示给子行,但是我希望它首先被过滤,具体取决于所选的行。我要检索的信息不在用于创建DataTable的原始数据源上。因此,我想将记录ID传递给直接从JavaScript使用的新查询。这是代码:

function kpi_values(d) {
    // `d` is the original data object for the row
    var $id = d.id;
    return '<table>'+
                '<thead>'+
                    '<tr>'+
                        '<th>Month</th>'+
                        '<th>Value</th>'+
                        '<th>Comment</th>'+
                    '</tr>'+
                '</thead>'+
                '<tbody>'+
                    '@foreach( \App\Reports::with("kpi")
                                            ->where("kpi_id",'+$id+')'+
                                            //this gives no records
                                            //->where("kpi_id",$id) -> this gives an error
                                            '->orderBy("month","desc")
                                            ->take(5)
                                            ->get() as $report)'+
                    '<tr>'+
                        '<td>{{$report->month}}</td>'+
                        '<td>{{$report->value}}</td>'+
                        '<td>{{$report->comment}}</td>'+
                    '</tr>'+
                    '@endforeach'+
                '<tbody>'+
            '</table>'
}


我收到的错误是:

ErrorException in e09c19f3dff5443f7b05b7c3c3e2f2a2 line 66:
Undefined variable: id


编辑1
我只是意识到var $id不是php变量,因此不会那样工作。所以我通过用var $id = d.id替换<?php $id = /*the problem*/ ?>更新了我的代码

但是我不能将d.id作为值传递给该变量。

最佳答案

我认为您无法像这样从JavaScript到PHP获取ID。因为PHP会呈现您的脚本并将其与HTML一起发送到浏览器中。

您应该为每一行编写一个ajax调用,或者在渲染为HTML之前准备数据

09-25 18:05
查看更多