如何在Laravel 5.2中访问数据表服务器端处理中的json对象/数据?

时间:2022-06-05 12:47:59

We're using with datatables on a new project and due to the large amount of data we are working with, we are using the server side processing of datatables.But right now we're trying to figure out how to get all the data from the server through datatables.As I use the Custom HTTP variables server side processing and as I take a look with the data returned, there are no values coming from the database. How could I access the data? Please help. Thanks a lot. Here are my code

我们正在使用新项目上的数据表,并且由于我们正在处理大量数据,我们正在使用数据表的服务器端处理。但是现在我们正试图弄清楚如何从中获取所有数据服务器通过datatables.As我使用自定义HTTP变量服务器端处理,当我看看返回的数据时,没有来自数据库的值。我怎样才能访问数据?请帮忙。非常感谢。这是我的代码

javascript:

JavaScript的:

$('#table-prod-contents').DataTable({
              processing: true,
              serverSide: true,
              ajax: $.fn.dataTable.pipeline( {
                  url: '{{ url("postproductsdata") }}',
                  pages: 6000, // number of pages to cache
                  "data": function ( d ) {
                     console.log(d);
                  }
              } ),
              columns: [
                  {data: 'id', name: 'id'},
                  {data: 'category', name: 'category'},
                  {data: 'pharmaceutical', name: 'pharmaceutical'},
                  {data: 'description', name: 'description'},
                  {data: 'type', name: 'type'},
                  {data: 'unit', name: 'unit'},
                  {data: 'price', name: 'price'},
                  {data: 'created_at', name: 'created_at'},
              ],


          });

The console,log only shows the data shown in the image below:

控制台,日志仅显示下图中显示的数据:

如何在Laravel 5.2中访问数据表服务器端处理中的json对象/数据?

如何在Laravel 5.2中访问数据表服务器端处理中的json对象/数据?

As I look inside the Object, there is no value of an id. Something like id: 31

当我查看Object时,没有id的值。像id这样的东西:31

Controller.php

Controller.php这样

public function anyData()
{
    $conditionTxt = "Medical and Lab Supplies";

    $products = Product::where('category', 'ILIKE', '%'.$conditionTxt.'%')
                        ->orderBy('created_at', 'desc')
                        ->get();

    return Datatables::of($products)->make(true);

}

1 个解决方案

#1


2  

Option ajax.data allows to define function to manipulate the data before it's sent to the server.

选项ajax.data允许定义在将数据发送到服务器之前操作数据的函数。

You could use ajax.dataSrc to get access to data received from the server, however you're also using pipelining which doesn't allow that.

您可以使用ajax.dataSrc来访问从服务器接收的数据,但是您也使用了不允许的流水线操作。

Use either drawCallback option along with ajax.json() API method to get access to retrieved data or createdRow option or xhr.dt event.

使用drawCallback选项和ajax.json()API方法来访问检索到的数据或createdRow选项或xhr.dt事件。

For example:

例如:

$('#table-prod-contents').DataTable({
    processing: true,
    serverSide: true,
    ajax: $.fn.dataTable.pipeline( {
        url: '{{ url("postproductsdata") }}',
        pages: 6000, // number of pages to cache
        "data": function ( d ) {
           console.log(d);
        }
    } ),
    drawCallback: function(settings){
       var api = this.api();

       console.log(api.ajax.json());
    },
    columns: [
        {data: 'id', name: 'id'},
        {data: 'category', name: 'category'},
        {data: 'pharmaceutical', name: 'pharmaceutical'},
        {data: 'description', name: 'description'},
        {data: 'type', name: 'type'},
        {data: 'unit', name: 'unit'},
        {data: 'price', name: 'price'},
        {data: 'created_at', name: 'created_at'},
    ],
});

#1


2  

Option ajax.data allows to define function to manipulate the data before it's sent to the server.

选项ajax.data允许定义在将数据发送到服务器之前操作数据的函数。

You could use ajax.dataSrc to get access to data received from the server, however you're also using pipelining which doesn't allow that.

您可以使用ajax.dataSrc来访问从服务器接收的数据,但是您也使用了不允许的流水线操作。

Use either drawCallback option along with ajax.json() API method to get access to retrieved data or createdRow option or xhr.dt event.

使用drawCallback选项和ajax.json()API方法来访问检索到的数据或createdRow选项或xhr.dt事件。

For example:

例如:

$('#table-prod-contents').DataTable({
    processing: true,
    serverSide: true,
    ajax: $.fn.dataTable.pipeline( {
        url: '{{ url("postproductsdata") }}',
        pages: 6000, // number of pages to cache
        "data": function ( d ) {
           console.log(d);
        }
    } ),
    drawCallback: function(settings){
       var api = this.api();

       console.log(api.ajax.json());
    },
    columns: [
        {data: 'id', name: 'id'},
        {data: 'category', name: 'category'},
        {data: 'pharmaceutical', name: 'pharmaceutical'},
        {data: 'description', name: 'description'},
        {data: 'type', name: 'type'},
        {data: 'unit', name: 'unit'},
        {data: 'price', name: 'price'},
        {data: 'created_at', name: 'created_at'},
    ],
});