130 lines
4.5 KiB
PHP
130 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\DataTables;
|
|
|
|
use App\Models\User;
|
|
use Yajra\DataTables\Html\Button;
|
|
use Yajra\DataTables\Html\Column;
|
|
use Yajra\DataTables\Services\DataTable;
|
|
|
|
class UsersDataTable extends DataTable
|
|
{
|
|
/**
|
|
* Build DataTable class.
|
|
*
|
|
* @param mixed $query Results from query() method.
|
|
* @return \Yajra\DataTables\DataTableAbstract
|
|
*/
|
|
public function dataTable($query)
|
|
{
|
|
return datatables()
|
|
->eloquent($query)
|
|
->editColumn('userProfile.country', function($query) {
|
|
return $query->userProfile->country ?? '-';
|
|
})
|
|
->editColumn('userProfile.company_name', function($query) {
|
|
return $query->userProfile->company_name ?? '-';
|
|
})
|
|
->editColumn('status', function($query) {
|
|
$status = 'warning';
|
|
switch ($query->status) {
|
|
case 'active':
|
|
$status = 'primary';
|
|
break;
|
|
case 'inactive':
|
|
$status = 'danger';
|
|
break;
|
|
case 'banned':
|
|
$status = 'dark';
|
|
break;
|
|
}
|
|
return '<span class="text-capitalize badge bg-'.$status.'">'.$query->status.'</span>';
|
|
})
|
|
->editColumn('created_at', function($query) {
|
|
return date('Y/m/d',strtotime($query->created_at));
|
|
})
|
|
->filterColumn('full_name', function($query, $keyword) {
|
|
$sql = "CONCAT(users.first_name,' ',users.last_name) like ?";
|
|
return $query->whereRaw($sql, ["%{$keyword}%"]);
|
|
})
|
|
->filterColumn('userProfile.company_name', function($query, $keyword) {
|
|
return $query->orWhereHas('userProfile', function($q) use($keyword) {
|
|
$q->where('company_name', 'like', "%{$keyword}%");
|
|
});
|
|
})
|
|
->filterColumn('userProfile.country', function($query, $keyword) {
|
|
return $query->orWhereHas('userProfile', function($q) use($keyword) {
|
|
$q->where('country', 'like', "%{$keyword}%");
|
|
});
|
|
})
|
|
->addColumn('action', 'users.action')
|
|
->rawColumns(['action','status']);
|
|
}
|
|
|
|
/**
|
|
* Get query source of dataTable.
|
|
*
|
|
* @param \App\Models\User $model
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function query()
|
|
{
|
|
$model = User::query()->with('userProfile');
|
|
return $this->applyScopes($model);
|
|
}
|
|
|
|
/**
|
|
* Optional method if you want to use html builder.
|
|
*
|
|
* @return \Yajra\DataTables\Html\Builder
|
|
*/
|
|
public function html()
|
|
{
|
|
return $this->builder()
|
|
->setTableId('dataTable')
|
|
->columns($this->getColumns())
|
|
->minifiedAjax()
|
|
->dom('<"row align-items-center"<"col-md-2" l><"col-md-6" B><"col-md-4"f>><"table-responsive my-3" rt><"row align-items-center" <"col-md-6" i><"col-md-6" p>><"clear">')
|
|
|
|
->parameters([
|
|
"processing" => true,
|
|
"autoWidth" => false,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get columns.
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function getColumns()
|
|
{
|
|
return [
|
|
['data' => 'id', 'name' => 'id', 'title' => 'id'],
|
|
['data' => 'full_name', 'name' => 'full_name', 'title' => 'FULL NAME', 'orderable' => false],
|
|
['data' => 'phone_number', 'name' => 'phone_number', 'title' => 'Phone Number'],
|
|
['data' => 'email', 'name' => 'email', 'title' => 'Email'],
|
|
['data' => 'userProfile.country', 'name' => 'userProfile.country', 'title' => 'Country'],
|
|
['data' => 'status', 'name' => 'status', 'title' => 'Status'],
|
|
['data' => 'userProfile.company_name', 'name' => 'userProfile.company_name', 'title' => 'Company'],
|
|
['data' => 'created_at', 'name' => 'created_at', 'title' => 'Join Date'],
|
|
Column::computed('action')
|
|
->exportable(false)
|
|
->printable(false)
|
|
->searchable(false)
|
|
->width(60)
|
|
->addClass('text-center hide-search'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get filename for export.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function filename()
|
|
{
|
|
return 'Users_' . date('YmdHis');
|
|
}
|
|
}
|