[英]Laravel datatable sorting issue
我正在使用 Laravel - yajra 的數據表https://github.com/yajra/laravel-datatables-docs
它與單個表完美配合,但是當我使用它的雄辯關系時事情變得嚴重。 如下代碼中所述,我在表中顯示用戶名,它顯示的用戶名非常好,但是當我嘗試對用戶進行排序或簡單地搜索時,它給了我錯誤的錯誤或顯示 SQL 錯誤。
我有以下雄辯的模型
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
<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' }
]
});
項目負責人
public function projects(){
return Datatables::of(Proejct::with(['client.user']))
->addColumn("client.user.name", function($row){
return $row->client->user->name;
})->make(true);
}
它不支持多層次的雄辯關系排序或搜索。 因此,您將獲得數據,但無法對其進行搜索或排序。
你的代碼看起來沒問題,它可以完美地用於單層。
這是有關該問題的更多詳細信息。 https://github.com/yajra/laravel-datatables/issues/993
返回 $row->client()->user()->name;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.