File: //home/armgroup/libs/app/DataTables/CommentsDataTable.php
<?php
namespace App\DataTables;
use App\Models\Comment;
use Illuminate\Database\Eloquent\Builder;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;
class CommentsDataTable extends DataTable
{
/**
* Build DataTable class.
*
* @param mixed $query Results from query() method.
* @return \Yajra\DataTables\DataTableAbstract
*/
public function dataTable($query)
{
return datatables()
->eloquent($query)
->addColumn('action', function ($query) {
$abilities = [
'show' => route('admin.comments.show', $query->id),
'delete' => route('admin.comments.destroy', $query->id),
];
return view('admin.partials.datatables-btns', compact('abilities'));
})
->editColumn('created_at', function ($query) {
return to_persian_numbers(jdate($query->created_at));
})
->editColumn('comment', function ($query) {
return words($query->comment, 5);
})
->editColumn('checked', function ($query) {
return view('admin.partials.datatables-status', [
'color' => ['danger', 'success'][$query->checked],
'name' => ['بررسی نشده', 'بررسی شده'][$query->checked],
]);
})
->editColumn('status', function ($query) {
return view('admin.partials.datatables-status', [
'color' => Comment::STATUS_COLOR[$query->status],
'name' => __(Comment::STATUS_TRANSLATE[$query->status]),
]);
})
->addIndexColumn();
}
/**
* Get query source of dataTable.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query(Comment $model)
{
$commentable_type = $this->request()->get('commentable_type') ? 'App\\Models\\'.$this->request()->get('commentable_type') : null;
$result = $model->newQuery();
if (auth()->user()->cannot('manage other users content')) {
$result = $result->whereRelation('commentable', 'user_id', auth()->id());
}
return $result->when(! is_null($commentable_type), function ($query) use ($commentable_type) {
return $query->where('commentable_type', $commentable_type);
});
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\DataTables\Html\Builder
*/
public function html()
{
return $this->builder()
->setTableId('comments-table')
->columns($this->getColumns())
->minifiedAjax()
->dom('Bfrtip')
->orderBy(6)
->language(asset('vendor/datatables/Persian.json'))
->buttons(
Button::make('excel'),
Button::make('print'),
Button::make('reset'),
Button::make('reload')
);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
Column::make('DT_RowIndex')
->title('#')
->orderable(false)
->searchable(false),
Column::make('name')->title('نام'),
Column::make('email')->title('ایمیل'),
Column::make('comment')->title('نظر'),
Column::make('checked')->title('وضعیت بررسی'),
Column::make('status')->title('وضعیت نظر'),
Column::make('created_at')->title('تاریخ'),
Column::computed('action')
->exportable(false)
->printable(false)
->title('عملیات'),
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'Comments_'.date('YmdHis');
}
}