多くのエンティティのテーブルに所有エンティティのIDを格納することによってアサートされた1対多の関係。 例:コーディネーターは1つまたは複数のセンターを管理します。 html Builderを使用して列を設定した後、coudinator_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;
})
コーディネーターの名で検索することはできますが、それで検索したいので、姓で両方を1つの列に連結する方法はありますか? むしろ、アクセサメソッドを適用しますか、それともデータテーブルに姓の列を含める必要がありますか?
よろしく、
あなたができるトリックは、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に両方の列を追加することですが、それらはまだ検索可能であることができます: