gpt4 book ai didi

Laravel 数据表排序不起作用如何在连接上使用

转载 作者:行者123 更新时间:2023-12-02 04:35:46 26 4
gpt4 key购买 nike

我正在使用 Laravel Datatable,排序不工作,谁能帮帮我。

Controller

Table::select(array( DB::raw('table2.con_title'),
DB::raw('........
Datatables::of(----)->make();

查看

.dataTable({ "bProcessing": true, "bServerSide": true, "sAjaxSource": ajaxurl,

"aoColumnDefs": [ { mData:'table2.con_title' , aTargets: [0]},.......

错误 DataTables 警告(表 id = '-----'):从第 0 行的数据源请求了未知参数“table2.con_title”

最佳答案

最近我在使用 Laravel 数据表并遇到了类似的情况,数据正在加载到列中但排序不起作用,我的数据表正在从多个数据库 (DB) 表加载数据。以下是我的发现:

  • 确保您的数据库表关系是按照文档设置的 https://laravel.com/docs/5.7/eloquent-relationships
  • 如果您使用的是 DB::raw($your_sql) - 请确保您在数据表列配置中引用了正确的数据库列名称。例如:

        $sql_query = "
    SELECT
    id AS primary_key,
    first_name,
    last_name
    FROM
    Contacts
    ";

    $data = collect(DB::select(DB::raw($sql_query)));
    $list = Datatables::of($data);
    $list->make(true);
  • 在您的 blade 文件中,像这样配置数据表列

            <table id="name-list">
    <thead>
    <tr>
    <th>ID</th>
    <th>First Name</th>
    <th>Last Name</th>
    </tr>
    </thead>
    <tbody>

    </tbody>
    </table>

    $('#name-list').dataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "{{route('path_to_your_server_code')}}",
    "columns": [
    {data: 'primary_key', name: 'primary_key', orderable: true, searchable: true, visible: true},
    {data: 'first_name', name: 'first_name', orderable: true, searchable: true, visible: true},
    {data: 'last_name', name: 'last_name', orderable: true, searchable: true, visible: true}],
    "order":[[1, 'desc']]
    });
  • 如果您在 SQL 中使用 Eloquent-Relationships 并急切加载多个关系(即多个数据库表),请确保您通过 Eloquent-Relationships 引用数据库列,例如关系.column_name。您的数据表列配置将如下所示:

         //column configuration
    {data: 'some_relation.db_column', name: 'some_relation.db_column', orderable: true, searchable: true, visible: true}

    //complete example code
    $('#name-list').dataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "{{route('path_to_your_server_code')}}",
    "columns": [
    {data: 'primary_key', name: 'primary_key', orderable: true, searchable: true, visible: true},
    {data: 'first_name', name: 'first_name', orderable: true, searchable: true, visible: true},
    {data: 'last_name', name: 'last_name', orderable: true, searchable: true, visible: true},
    {data: 'some_relation.db_column', name: 'some_relation.db_column', orderable: true, searchable: true, visible: true}],
    "order":[[1, 'desc']]
    });

我已经尝试了以上两种方式,并且排序在两种情况下都对我有用,即使用 DB::raw($your_sql) 和 Eloquent-Relationships。

关于Laravel 数据表排序不起作用如何在连接上使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43367498/

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