简体   繁体   English

Laravel 数据表排序问题

[英]Laravel datatable sorting issue

I'm using Laravel - data tables by yajra https://github.com/yajra/laravel-datatables-docs我正在使用 Laravel - yajra 的数据表https://github.com/yajra/laravel-datatables-docs

It works perfectly with a single table, but things get serious when I use the eloquent relationship with it.它与单个表完美配合,但是当我使用它的雄辩关系时事情变得严重。 As described below in code I'm showing user name in the table, it showing user name perfectly fine but when I try to sort on User or simply search it gives me a wrong error or shows SQL error.如下代码中所述,我在表中显示用户名,它显示的用户名非常好,但是当我尝试对用户进行排序或简单地搜索时,它给了我错误的错误或显示 SQL 错误。

I have following eloquent models我有以下雄辩的模型

class Project extends Model{
     public function client(){
      return $this->belongsTo(Client::class);
     }
}
class Client extends Model{
     public function user(){
          return $this->belongsTo(User::class);
     }
}
class User extends Model{
}

HTML HTML

<table id="table">
    <tbody>
        <tr>
            <td>Name</td>
            <td>Start Date</td>
            <td>Target</td>
            <td>User</td>
        </tr>
    </tbody>
</table>

Javascript

$("#table").DataTable({
        processing: true,
        serverSide: true,
        autoWidth:false,
        ajax: '/projects',
        columns:[
            { data: 'project_name', name: 'project_name' },
            { data: 'start_date', name: 'start_date' },
            { data: 'target', name: 'target' },
            { data: 'client.user.name', name: 'client.user.name' }
        ]
    });

Project Controller项目负责人

public function projects(){
    return Datatables::of(Proejct::with(['client.user']))
            ->addColumn("client.user.name", function($row){
                return $row->client->user->name;
            })->make(true);
}

It doesn't not support multi level of eloquent relationship sorting or search.它不支持多层次的雄辩关系排序或搜索。 So you will get data but you can't search or sort on them.因此,您将获得数据,但无法对其进行搜索或排序。

Your code seems okay and it will work perfectly for single level.你的代码看起来没问题,它可以完美地用于单层。

Here's more details about the issue.这是有关该问题的更多详细信息。 https://github.com/yajra/laravel-datatables/issues/993 https://github.com/yajra/laravel-datatables/issues/993

返回 $row->client()->user()->name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM