tiers = TierModel::orderBy('min_points', 'asc') ->get() ->map(fn ($tier) => [ 'hash' => $tier->hash, 'name' => $tier->name, 'min_points' => currency($tier->min_points, ''), 'max_points' => currency($tier->max_points, ''), ]) ->toArray(); } #[On('modal:open')] public function openModal(string $method, string $modalTitle, ?string $id = null) { $this->resetValidation(); $this->resetErrorBag(); $this->method = $method; $this->modalTitle = $modalTitle; if ($id) { $this->form->setTier(TierModel::byHashOrFail($id)); } } public function create() { $this->canOrAbort('create tier'); $tier = $this->form->store(); $tiers = $this->tiers; $tiers[] = [ 'hash' => $tier->hash, 'name' => $tier->name, 'min_points' => currency($tier->min_points, ''), 'max_points' => currency($tier->max_points, ''), ]; usort($tiers, fn ($a, $b) => replaceCurrency($a['min_points']) <=> replaceCurrency($b['min_points'])); $this->tiers = $tiers; $this->toast('Tier berhasil ditambahkan.'); Flux::modals()->close(); } public function update() { $this->canOrAbort('update tier'); $tier = $this->form->update(); // update array tiers $tiers = $this->tiers; foreach ($tiers as $index => $item) { if ($item['hash'] === $tier->hash) { $tiers[$index] = [ 'hash' => $tier->hash, 'name' => $tier->name, 'min_points' => currency($tier->min_points, ''), 'max_points' => currency($tier->max_points, ''), ]; break; } } $this->tiers = $tiers; $this->toast('Tier berhasil diperbarui.'); Flux::modals()->close(); } public function delete(TierModel $tier) { $tier->delete(); // update array tiers $tiers = array_values(array_filter($this->tiers, fn ($item) => $item['hash'] !== $tier->hash)); $this->tiers = $tiers; $this->toast('Tier berhasil dihapus.'); Flux::modals()->close(); } public function render() { return view('livewire.studio.loyalty.tiers', [ 'pageTitle' => 'Tier', ]); } }