feat(user): sync user dengan outlet saat crud dan menampilkan outlet di tabel
This commit is contained in:
parent
e9336d37c8
commit
741143a407
@ -11,6 +11,7 @@
|
|||||||
use Illuminate\Support\Facades\Blade;
|
use Illuminate\Support\Facades\Blade;
|
||||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||||
|
use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn;
|
||||||
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
|
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
|
||||||
|
|
||||||
class UsersTable extends DataTableComponent
|
class UsersTable extends DataTableComponent
|
||||||
@ -80,6 +81,11 @@ public function columns(): array
|
|||||||
|
|
||||||
Column::make('Alamat', 'address')->searchable(),
|
Column::make('Alamat', 'address')->searchable(),
|
||||||
|
|
||||||
|
ArrayColumn::make('Outlet')
|
||||||
|
->data(fn ($value, $row) => $row->user->outlets->pluck('name')->toArray())
|
||||||
|
->outputFormat(fn ($index, $value) => Blade::render('<span class="text-xs text-gray-400 border border-gray-400 rounded px-1">'.$value.'</span>'))
|
||||||
|
->flexRow(['class' => 'gap-2 flex-wrap']),
|
||||||
|
|
||||||
Column::make('Tanggal')
|
Column::make('Tanggal')
|
||||||
->label(function ($row) {
|
->label(function ($row) {
|
||||||
$birthdate = formatDate($row->birthdate);
|
$birthdate = formatDate($row->birthdate);
|
||||||
@ -162,6 +168,7 @@ public function builder(): Builder
|
|||||||
->with([
|
->with([
|
||||||
'user',
|
'user',
|
||||||
'user.referralCode',
|
'user.referralCode',
|
||||||
|
'user.outlets',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -38,6 +38,8 @@ class UserForm extends Form
|
|||||||
|
|
||||||
public string $status = '1';
|
public string $status = '1';
|
||||||
|
|
||||||
|
public array $outlet_ids = [];
|
||||||
|
|
||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@ -51,6 +53,8 @@ public function rules(): array
|
|||||||
'address' => ['required', 'string'],
|
'address' => ['required', 'string'],
|
||||||
'gender' => ['required', Rule::in(Gender::cases())],
|
'gender' => ['required', Rule::in(Gender::cases())],
|
||||||
'status' => ['required', Rule::in(UserStatus::cases())],
|
'status' => ['required', Rule::in(UserStatus::cases())],
|
||||||
|
'outlet_ids' => ['required', 'array', 'min:1'],
|
||||||
|
'outlet_ids.*' => Rule::exists('outlets', 'id'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,11 +69,14 @@ public function validationAttributes(): array
|
|||||||
'hire_date' => 'tanggal bergabung',
|
'hire_date' => 'tanggal bergabung',
|
||||||
'address' => 'alamat',
|
'address' => 'alamat',
|
||||||
'gender' => 'jenis kelamin',
|
'gender' => 'jenis kelamin',
|
||||||
|
'outlet_ids' => 'outlet',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUser(User $user)
|
public function setUser(User $user)
|
||||||
{
|
{
|
||||||
|
$user->load(['employee', 'outlets']);
|
||||||
|
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
$employee = $user->employee;
|
$employee = $user->employee;
|
||||||
|
|
||||||
@ -83,6 +90,7 @@ public function setUser(User $user)
|
|||||||
$this->address = $employee->address;
|
$this->address = $employee->address;
|
||||||
$this->gender = $employee->gender->value;
|
$this->gender = $employee->gender->value;
|
||||||
$this->status = $user->status->value;
|
$this->status = $user->status->value;
|
||||||
|
$this->outlet_ids = $user->outlets->pluck('id')->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store()
|
public function store()
|
||||||
@ -116,6 +124,8 @@ public function store()
|
|||||||
'address' => $data['address'],
|
'address' => $data['address'],
|
||||||
'gender' => $data['gender'],
|
'gender' => $data['gender'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$user->outlets()->attach($this->outlet_ids);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,6 +157,8 @@ public function update()
|
|||||||
'address' => $data['address'],
|
'address' => $data['address'],
|
||||||
'gender' => $data['gender'],
|
'gender' => $data['gender'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$this->user->outlets()->sync($this->outlet_ids);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,9 @@
|
|||||||
namespace App\Livewire\Studio\Master\User;
|
namespace App\Livewire\Studio\Master\User;
|
||||||
|
|
||||||
use App\Livewire\Forms\Studio\Master\UserForm;
|
use App\Livewire\Forms\Studio\Master\UserForm;
|
||||||
|
use App\Models\Outlet;
|
||||||
use App\Traits\WithAuthorization;
|
use App\Traits\WithAuthorization;
|
||||||
|
use App\Traits\WithOutletSelector;
|
||||||
use App\Traits\WithToast;
|
use App\Traits\WithToast;
|
||||||
use App\Traits\WithUpdatedData;
|
use App\Traits\WithUpdatedData;
|
||||||
use Livewire\Attributes\Title;
|
use Livewire\Attributes\Title;
|
||||||
@ -12,10 +14,17 @@
|
|||||||
#[Title('Tambah Pegawai')]
|
#[Title('Tambah Pegawai')]
|
||||||
class Create extends Component
|
class Create extends Component
|
||||||
{
|
{
|
||||||
use WithAuthorization, WithToast, WithUpdatedData;
|
use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData;
|
||||||
|
|
||||||
public UserForm $form;
|
public UserForm $form;
|
||||||
|
|
||||||
|
public array $outlets = [];
|
||||||
|
|
||||||
|
public function mount()
|
||||||
|
{
|
||||||
|
$this->outlets = Outlet::pluck('name', 'id')->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
public function save()
|
public function save()
|
||||||
{
|
{
|
||||||
$this->canOrAbort('create user');
|
$this->canOrAbort('create user');
|
||||||
|
|||||||
@ -3,8 +3,10 @@
|
|||||||
namespace App\Livewire\Studio\Master\User;
|
namespace App\Livewire\Studio\Master\User;
|
||||||
|
|
||||||
use App\Livewire\Forms\Studio\Master\UserForm;
|
use App\Livewire\Forms\Studio\Master\UserForm;
|
||||||
|
use App\Models\Outlet;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Traits\WithAuthorization;
|
use App\Traits\WithAuthorization;
|
||||||
|
use App\Traits\WithOutletSelector;
|
||||||
use App\Traits\WithToast;
|
use App\Traits\WithToast;
|
||||||
use App\Traits\WithUpdatedData;
|
use App\Traits\WithUpdatedData;
|
||||||
use Livewire\Attributes\Title;
|
use Livewire\Attributes\Title;
|
||||||
@ -13,13 +15,16 @@
|
|||||||
#[Title('Ubah Pegawai')]
|
#[Title('Ubah Pegawai')]
|
||||||
class Edit extends Component
|
class Edit extends Component
|
||||||
{
|
{
|
||||||
use WithAuthorization, WithToast, WithUpdatedData;
|
use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData;
|
||||||
|
|
||||||
public UserForm $form;
|
public UserForm $form;
|
||||||
|
|
||||||
|
public array $outlets = [];
|
||||||
|
|
||||||
public function mount(User $user)
|
public function mount(User $user)
|
||||||
{
|
{
|
||||||
$this->form->setUser($user);
|
$this->form->setUser($user);
|
||||||
|
$this->outlets = Outlet::pluck('name', 'id')->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function save()
|
public function save()
|
||||||
|
|||||||
@ -119,6 +119,24 @@
|
|||||||
<flux:error name="form.status" />
|
<flux:error name="form.status" />
|
||||||
</flux:field>
|
</flux:field>
|
||||||
</flux:card>
|
</flux:card>
|
||||||
|
|
||||||
|
<flux:card class="space-y-6 p-6">
|
||||||
|
<flux:field>
|
||||||
|
<flux:label>Outlet <span class="text-red-500 ms-1">*</span></flux:label>
|
||||||
|
<flux:select variant="listbox" multiple searchable placeholder="Pilih Outlet"
|
||||||
|
wire:model.live.debounce.500ms="form.outlet_ids">
|
||||||
|
<flux:select.option wire:click="selectAllOutlets" wire:ignore>Pilih Semua
|
||||||
|
</flux:select.option>
|
||||||
|
<flux:select.option wire:click="deselectAllOutlets" wire:ignore>Hapus Semua
|
||||||
|
</flux:select.option>
|
||||||
|
@foreach ($outlets as $key => $value)
|
||||||
|
<flux:select.option value="{{ $key }}">{{ $value }}
|
||||||
|
</flux:select.option>
|
||||||
|
@endforeach
|
||||||
|
</flux:select>
|
||||||
|
<flux:error name="form.outlet_ids" />
|
||||||
|
</flux:field>
|
||||||
|
</flux:card>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user