user()->isAdmin(), 403); } public function edit(int $id): void { $model = Equipment::query()->findOrFail($id); $this->editingId = $model->id; $this->name = $model->name; $this->serialNumber = $model->serial_number; $this->inventoryNumber = $model->inventory_number; $this->categoryId = (string) $model->category_id; $this->clientId = (string) $model->client_id; $this->purchaseDate = (string) $model->purchase_date; $this->warrantyUntil = (string) $model->warranty_until; $this->condition = $model->condition->value; $this->notes = (string) $model->notes; } public function save(): void { $serialRule = Rule::unique('equipment', 'serial_number'); $inventoryRule = Rule::unique('equipment', 'inventory_number'); if ($this->editingId) { $serialRule = $serialRule->ignore($this->editingId); $inventoryRule = $inventoryRule->ignore($this->editingId); } $this->validate([ 'name' => ['required', 'string', 'max:255'], 'serialNumber' => ['required', 'string', $serialRule], 'inventoryNumber' => ['required', 'string', $inventoryRule], 'categoryId' => ['required', 'exists:equipment_categories,id'], 'clientId' => ['nullable', 'exists:users,id'], 'purchaseDate' => ['nullable', 'date'], 'warrantyUntil' => ['nullable', 'date'], 'condition' => ['required', Rule::in(['good', 'fair', 'poor'])], 'notes' => ['nullable', 'string'], ]); Equipment::query()->updateOrCreate( ['id' => $this->editingId], [ 'name' => $this->name, 'serial_number' => $this->serialNumber, 'inventory_number' => $this->inventoryNumber, 'category_id' => $this->categoryId, 'client_id' => $this->clientId ?: null, 'purchase_date' => $this->purchaseDate ?: null, 'warranty_until' => $this->warrantyUntil ?: null, 'condition' => $this->condition, 'notes' => $this->notes, ] ); $this->reset(['name', 'serialNumber', 'inventoryNumber', 'categoryId', 'clientId', 'purchaseDate', 'warrantyUntil', 'notes', 'editingId']); $this->condition = 'good'; session()->flash('success', 'Оборудование сохранено.'); } public function delete(int $id): void { Equipment::query()->findOrFail($id)->delete(); session()->flash('success', 'Оборудование удалено.'); } public function render() { return view('livewire.directories.equipment-page', [ 'items' => Equipment::query()->with(['category', 'client'])->latest()->paginate(15), 'categories' => EquipmentCategory::query()->orderBy('name')->get(), 'clients' => User::query()->where('role', 'client')->orderBy('name')->get(), ])->title('Справочник: Оборудование'); } }