通过在许多实体的表中存储拥有实体的 id 来断言一对多关系。 例如:协调员管理一个或多个中心。 通过 html 构建器设置列后,我编辑 cooditor_id 列并应用访问器方法来检索协调器全名:
public function getFullNameAttribute()
{
return $this->first_name . ", " . $this->last_name;
}
我的目标是只允许属于经过身份验证的协调员的中心显示在数据表上,除非经过身份验证的用户是我的查询中显示的管理员。
但是,如果我输入出现在数据表上的 Coordinator 列的值,它会显示没有找到匹配的记录。
我如何实现这一目标?
请在此处描述您的问题/功能请求。<?php
namespace App\DataTables;
use Auth;
use App\Center;
use App\Coordinator;
use Yajra\Datatables\Services\DataTable;
class CentersDataTable extends DataTable
{
// protected $printPreview = 'path.to.print.preview.view';
/**
* Display ajax response.
*
* <strong i="13">@return</strong> \Illuminate\Http\JsonResponse
*/
public function ajax()
{
return $this->datatables
->eloquent($this->query())
->editColumn('action', function ($center) {
return '<a href="edit-center/'.$center->id.'" <i class="material-icons sideicons-tables">create</i>';
// <a href="field-technician-delete/'.$user->id.'" <i class="material-icons sideicons-tables">delete</i>
})
->editColumn('coordinator_id', function ($user) {
return Coordinator::find($user->coordinator_id)->full_name;
})
->editColumn('gps_lat', function ($center) {
return $center->gps_location ;
})
->make(true);
}
/**
* Get the query object to be processed by datatables.
*
* <strong i="14">@return</strong> \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
*/
public function query()
{
if(Auth::user()->hasRole('admin')){
$centers = Center::query();
return $this->applyScopes($centers);
}
else{
$centers = Center::query()->where('coordinator_id',Auth::user()->userable->id);
return $this->applyScopes($centers);
}
}
/**
* Optional method if you want to use html builder.
*
* <strong i="15">@return</strong> \Yajra\Datatables\Html\Builder
*/
public function html()
{
return $this->builder()
->columns([
'id' => ['title' => 'ID'],
'center_name' => ['title' => 'Name'],
'gps_lat' => ['title' => 'Coordinates'],
'contact_no' => ['title' => 'Contact Number'],
'coordinator_id' => [
'title' => 'Coordinator',
'searchable' => true
],
'created_at' => [ 'title' => 'Created'],
'updated_at' => ['title' => 'Updated'],
'action' => [
'data' => 'action',
'name' => 'action',
'render' => null,
'orderable' => false,
'searchable' => false,
'exportable' => false,
'printable' => false
]
])
->parameters([
'dom' => 'Bfrtip',
'buttons' => ['csv', 'excel', 'print','reload'],
]);
}
/**
* Get columns.
*
* <strong i="16">@return</strong> array
*/
private function getColumns()
{
return [
'id',
// add your columns
'created_at',
'updated_at',
];
}
/**
* Get filename for export.
*
* <strong i="17">@return</strong> string
*/
protected function filename()
{
return 'centers';
}
}
问候,
我尝试了急切加载,根据您网站上的教程进行了修改。 但是,现在我的问题是我没有存储协调员的全名,而是分别存储他们的名字和姓氏。 我目前拥有的:
询问
$centers = Center::query()->with('coordinator')->where('coordinator_id',Auth::user()->userable->id);
return $this->applyScopes($centers);
列
'first_name' => [
'data' => 'coordinator.first_name',
'name' => 'coordinator.first_name',
'title' => 'Coordinator',
'searchable' => true,
],
编辑方法
->editColumn('first_name', function ($user) {
return Area_Coordinator::find($user->area_coordinator_id)->full_name;
})
但是,我可以通过协调员的名字进行搜索,因为我想通过该名字进行搜索,而姓氏是否可以将两者连接到一列中? 或者更确切地说,应用我的访问器方法,还是应该只在数据表中包含姓氏列?
问候,
您可以做的一个技巧是在 js 上添加两列,但它们仍然可以搜索:
'last_name' => [
'data' => 'coordinator.last_name',
'visible' => false,
'name' => 'coordinator.last_name',
],
'coordinator' => [
'data' => 'coordinator',
'name' => 'coordinator.first_name',
],
->addColumn('coordinator', function ($user) {
return Area_Coordinator::find($user->area_coordinator_id)->full_name;
})
最有用的评论
您可以做的一个技巧是在 js 上添加两列,但它们仍然可以搜索: