HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: /home/armgroup/libs/app/Http/Livewire/Admin/Attribute/Edit.php
<?php

namespace App\Http\Livewire\Admin\Attribute;

use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Attribute;
use App\Models\AttributeValue;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

class Edit extends Component
{
    use ConvertEmptyStringsToNull, General;

    public Attribute $attribute;

    public $attributeGroupId;

    public $values = [];

    public $deleteIds = [];

    public function mount()
    {
        $this->values = $this->attribute
            ->values()
            ->select('id', 'attribute_id', 'value')
            ->get()
            ->toArray();
        $this->attributeGroupId = $this->attribute->group_id;
    }

    public function render()
    {
        return view('livewire.admin.attribute.edit');
    }

    protected function rules()
    {
        return ['values.*.value' => 'required|max:250|string|distinct'];
    }

    protected $validationAttributes = ['values.*.value' => 'مقدار'];

    public function addItem()
    {
        array_push($this->values, [
            'id' => null,
            'attribute_id' => $this->attribute->id,
            'value' => null,
        ]);
    }

    public function removeItem($index)
    {
        if ($this->values[$index]['id']) {
            $this->deleteIds[] = $this->values[$index]['id'];
        }

        unset($this->values[$index]);
    }

    public function update()
    {
        $this->validate();
        DB::transaction(function () {
            if (! count($this->values)) {
                $this->attribute->values()->delete();
            } else {
                if (count($this->deleteIds)) {
                    AttributeValue::whereIn('id', $this->deleteIds)->delete();
                }

                AttributeValue::upsert(
                    $this->values,
                    ['id', 'attribute_id'],
                    ['value']
                );
            }
            saveActivity("مقادیر ویژگی {$this->attribute->name} ویرایش شد", $this->attribute);
            $this->doneMessage("مقادیر ویژگی {$this->attribute->name} با موفقیت ویرایش شد");
        });

        return redirect()->route('admin.attribute-groups.show', $this->attributeGroupId);
    }
}