fix(user): menukar select model tabel UsersTable dari User ke Employee dan menyesuiakn kolom nya
This commit is contained in:
parent
f68875c6ad
commit
2a0e14a69f
@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Livewire\Datatable;
|
namespace App\Livewire\Datatable;
|
||||||
|
|
||||||
use App\Enums\Gender;
|
|
||||||
use App\Enums\UserStatus;
|
use App\Enums\UserStatus;
|
||||||
use App\Models\User;
|
use App\Models\Employee;
|
||||||
use App\Traits\Datatable\WithAppendColumn;
|
use App\Traits\Datatable\WithAppendColumn;
|
||||||
use App\Traits\Datatable\WithConfiguration;
|
use App\Traits\Datatable\WithConfiguration;
|
||||||
use App\Traits\Datatable\WithPrependColumn;
|
use App\Traits\Datatable\WithPrependColumn;
|
||||||
@ -18,74 +17,79 @@ class UsersTable extends DataTableComponent
|
|||||||
{
|
{
|
||||||
use WithAppendColumn, WithConfiguration, WithPrependColumn;
|
use WithAppendColumn, WithConfiguration, WithPrependColumn;
|
||||||
|
|
||||||
protected $model = User::class;
|
protected $model = Employee::class;
|
||||||
|
|
||||||
public function columns(): array
|
public function columns(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
Column::make('Nama Lengkap')
|
Column::make('Nama Lengkap')
|
||||||
->label(function ($row) {
|
->label(
|
||||||
return <<<HTML
|
fn ($row) => <<<HTML
|
||||||
<div>
|
<div>
|
||||||
<div>{$row->employee?->full_name}</div>
|
<div>{$row->full_name}</div>
|
||||||
<span class="text-xs text-gray-400">{$row->employee?->code}</span>
|
<span class="text-xs text-gray-400">{$row->code}</span>
|
||||||
</div>
|
</div>
|
||||||
HTML;
|
HTML
|
||||||
})
|
)
|
||||||
->searchable(function ($query, $searchTerm) {
|
->searchable(
|
||||||
$query->whereHas('employee', function ($query) use ($searchTerm) {
|
fn ($query, $searchTerm) => $query->where('full_name', 'like', "%{$searchTerm}%")
|
||||||
$query->where('full_name', 'like', "%{$searchTerm}%")
|
->orWhere('code', 'like', "%{$searchTerm}%")
|
||||||
->orWhere('code', 'like', "%{$searchTerm}%");
|
)
|
||||||
});
|
|
||||||
})
|
|
||||||
->html(),
|
->html(),
|
||||||
|
|
||||||
Column::make('Kredensial')
|
Column::make('Kredensial')
|
||||||
->label(function ($row) {
|
->label(
|
||||||
return <<<HTML
|
fn ($row) => <<<HTML
|
||||||
<div>
|
<div>
|
||||||
<div>{$row->email}</div>
|
<div>{$row?->user?->email}</div>
|
||||||
<span class="text-xs text-gray-400">{$row->username}</span>
|
<span class="text-xs text-gray-400">{$row?->user?->username}</span>
|
||||||
</div>
|
</div>
|
||||||
HTML;
|
HTML
|
||||||
})
|
)
|
||||||
->searchable(function ($query, $searchTerm) {
|
->searchable(
|
||||||
$query->where('email', 'like', "%{$searchTerm}%")
|
fn ($query, $searchTerm) => $query->whereHas(
|
||||||
->orWhere('username', 'like', "%{$searchTerm}%");
|
'user',
|
||||||
})
|
fn ($q) => $q->where('email', 'like', "%{$searchTerm}%")
|
||||||
|
->orWhere('username', 'like', "%{$searchTerm}%")
|
||||||
|
)
|
||||||
|
)
|
||||||
->html(),
|
->html(),
|
||||||
|
|
||||||
Column::make('Nomor Telepon', 'employee.phone_number')->searchable(),
|
Column::make('Nomor Telepon', 'phone_number')->searchable(),
|
||||||
|
|
||||||
Column::make('Jenis Kelamin', 'employee.gender')
|
Column::make('Jenis Kelamin', 'gender')
|
||||||
->format(fn ($value) => Blade::render(
|
->format(fn ($value) => Blade::render(
|
||||||
'<flux:badge color="'.Gender::from($value)->color().'">'
|
'<flux:badge color="'.$value->color().'">'
|
||||||
.Gender::from($value)->label()
|
.$value->label()
|
||||||
.'</flux:badge>'
|
.'</flux:badge>'
|
||||||
))
|
))
|
||||||
->html(),
|
->html(),
|
||||||
|
|
||||||
Column::make('Kode Referral', 'referralCode.code')
|
Column::make('Kode Referral', 'user.referralCode.code')
|
||||||
->format(fn ($value) => Blade::render('<flux:badge color="zinc">'.$value.'</flux:badge>'))
|
->format(
|
||||||
|
fn ($value) => $value
|
||||||
|
? Blade::render('<flux:badge color="zinc">'.$value.'</flux:badge>')
|
||||||
|
: ''
|
||||||
|
)
|
||||||
->html(),
|
->html(),
|
||||||
|
|
||||||
Column::make('Gaji Pokok', 'employee.base_salary')
|
Column::make('Gaji Pokok', 'base_salary')
|
||||||
->searchable()
|
->searchable()
|
||||||
->sortable()
|
->sortable()
|
||||||
->format(fn ($value) => currency($value, 'Rp')),
|
->format(fn ($value) => currency($value, 'Rp')),
|
||||||
|
|
||||||
Column::make('Alamat', 'employee.address')->searchable(),
|
Column::make('Alamat', 'address')->searchable(),
|
||||||
|
|
||||||
Column::make('Tanggal')
|
Column::make('Tanggal')
|
||||||
->label(function ($row) {
|
->label(function ($row) {
|
||||||
$birthdate = formatDate($row->employee->birthdate);
|
$birthdate = formatDate($row->birthdate);
|
||||||
$birthAgo = getAge($row->employee->birthdate);
|
$birthAgo = getAge($row->birthdate);
|
||||||
|
|
||||||
$hireDate = formatDate($row->hire_date);
|
$hireDate = formatDate($row?->user?->hire_date);
|
||||||
$openedAgo = timeAgo($row->hire_date);
|
$openedAgo = timeAgo($row?->user?->hire_date);
|
||||||
|
|
||||||
$resigndate = formatDate($row->resign_date);
|
$resigndate = formatDate($row?->user?->resign_date);
|
||||||
$resignAgo = timeAgo($row->resign_date);
|
$resignAgo = timeAgo($row?->user?->resign_date);
|
||||||
|
|
||||||
return <<<HTML
|
return <<<HTML
|
||||||
<div class="flex flex-col text-xs text-gray-700 dark:text-gray-300 gap-0.5">
|
<div class="flex flex-col text-xs text-gray-700 dark:text-gray-300 gap-0.5">
|
||||||
@ -109,15 +113,17 @@ public function columns(): array
|
|||||||
})
|
})
|
||||||
->html(),
|
->html(),
|
||||||
|
|
||||||
Column::make('status')
|
Column::make('Status', 'user.status')
|
||||||
->format(fn ($value) => Blade::render('<flux:badge color="'.$value->color().'">'.$value->label().'</flux:badge>'))
|
->format(
|
||||||
|
fn ($value) => Blade::render('<flux:badge color="'.UserStatus::tryFrom($value)->color().'">'.UserStatus::tryFrom($value)->label().'</flux:badge>')
|
||||||
|
)
|
||||||
->html(),
|
->html(),
|
||||||
|
|
||||||
Column::make('Aksi')
|
Column::make('Aksi')
|
||||||
->label(function ($row) {
|
->label(function ($row) {
|
||||||
$actions = '';
|
$actions = '';
|
||||||
|
|
||||||
if (auth()->id() !== $row->id) {
|
if (auth()->id() !== $row?->user?->id) {
|
||||||
$actions .= view('components.datatables.edit', [
|
$actions .= view('components.datatables.edit', [
|
||||||
'id' => $row->id,
|
'id' => $row->id,
|
||||||
'editRoute' => route('studio.master.user.edit', $row->id),
|
'editRoute' => route('studio.master.user.edit', $row->id),
|
||||||
@ -137,7 +143,22 @@ public function columns(): array
|
|||||||
|
|
||||||
public function builder(): Builder
|
public function builder(): Builder
|
||||||
{
|
{
|
||||||
return User::select('users.id', 'email', 'username', 'users.status', 'employee.code', 'employee.full_name', 'employee.phone_number', 'employee.base_salary', 'employee.address', 'employee.hire_date', 'employee.resign_date', 'employee.birthdate', 'employee.gender')->with(['employee', 'referralCode']);
|
return Employee::query()
|
||||||
|
->select(
|
||||||
|
'employees.id',
|
||||||
|
'employees.user_id',
|
||||||
|
'employees.code',
|
||||||
|
'employees.full_name',
|
||||||
|
'employees.phone_number',
|
||||||
|
'employees.gender',
|
||||||
|
'employees.birthdate',
|
||||||
|
'employees.base_salary',
|
||||||
|
'employees.address'
|
||||||
|
)
|
||||||
|
->with([
|
||||||
|
'user',
|
||||||
|
'user.referralCode',
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function filters(): array
|
public function filters(): array
|
||||||
@ -145,12 +166,15 @@ public function filters(): array
|
|||||||
return [
|
return [
|
||||||
MultiSelectFilter::make('Status')
|
MultiSelectFilter::make('Status')
|
||||||
->options(
|
->options(
|
||||||
collect(Userstatus::cases())
|
collect(UserStatus::cases())
|
||||||
->mapWithKeys(fn ($case) => [$case->value => $case->label()])
|
->mapWithKeys(fn ($case) => [$case->value => $case->label()])
|
||||||
->toArray()
|
->toArray()
|
||||||
)
|
)
|
||||||
->filter(function (Builder $builder, array $values) {
|
->filter(function (Builder $builder, array $values) {
|
||||||
$builder->whereIn('users.status', $values);
|
$builder->whereHas(
|
||||||
|
'user',
|
||||||
|
fn ($q) => $q->whereIn('status', $values)
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user