λ§μ μν°ν°μ ν μ΄λΈμ μμ μν°ν°μ IDλ₯Ό μ μ₯νμ¬ μ£Όμ₯νλ μΌλλ€ κ΄κ³. μ: μ½λλ€μ΄ν°κ° νλ μ΄μμ μΌν°λ₯Ό κ΄λ¦¬ν©λλ€. html λΉλλ₯Ό ν΅ν΄ μ΄μ μ€μ ν ν coodinator_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μ λ μ΄μ λͺ¨λ μΆκ°νμ§λ§ μ¬μ ν κ²μν μ μλλ‘ μΆκ°νλ κ²μ λλ€.