feat(bottle): menambahkan input outlet_ids

This commit is contained in:
Yoga Pangestu 2025-10-02 10:39:40 +07:00
parent 8d67c59f10
commit 8c411af7e5

View File

@ -6,6 +6,7 @@
use App\Rules\UnsignedInteger;
use App\Traits\WithMediaHandler;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Form;
class BottleForm extends Form
@ -24,6 +25,8 @@ class BottleForm extends Form
public ?string $description = null;
public array $outlet_ids = [];
public array $image = [];
public function rules(): array
@ -34,6 +37,8 @@ public function rules(): array
'cost_price' => ['required', 'numeric', new UnsignedInteger],
'sale_price' => ['required', 'numeric', new UnsignedInteger],
'description' => ['nullable', 'string'],
'outlet_ids' => ['required', 'array', 'min:1'],
'outlet_ids.*' => Rule::exists('outlets', 'id'),
'image' => ['nullable', 'array'],
];
}
@ -46,6 +51,7 @@ public function validationAttributes(): array
'cost_price' => 'harga beli',
'sale_price' => 'harga jual',
'description' => 'deskripsi',
'outlet_ids' => 'outlet',
'image' => 'gambar',
];
}
@ -59,7 +65,7 @@ public function setBottle(Bottle $bottle)
$this->cost_price = $bottle->cost_price;
$this->sale_price = $bottle->sale_price;
$this->description = $bottle->description;
$this->outlet_ids = $this->bottle->outlets->pluck('id')->toArray();
$this->image = $this->mapMediaCollection($bottle->getMedia('image'));
}
@ -76,6 +82,8 @@ public function store()
'description' => $this->description,
]);
$bottle->outlets()->attach($this->outlet_ids);
$this->uploadMedia($this->image, $bottle, 'image');
});
}
@ -93,6 +101,8 @@ public function update()
'description' => $this->description,
]);
$this->bottle->outlets()->attach($this->outlet_ids);
$this->syncMedia($this->image, $this->bottle, 'image');
$this->uploadMedia($this->image, $this->bottle, 'image');
});