File: /home/armgroup/libs/app/Http/Livewire/Admin/Brand/Edit.php
<?php
namespace App\Http\Livewire\Admin\Brand;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Brand;
use Livewire\Component;
class Edit extends Component
{
use ConvertEmptyStringsToNull, General;
public Brand $brand;
public $faq;
protected $listeners = ['imageChanged'];
public function mount()
{
$this->faq = $this->brand->faq ?: [];
}
protected function rules()
{
return [
'brand.name' => 'required|string|max:250',
'brand.slug' => 'required|string|max:250|alpha_dash|unique:brands,slug,'.$this->brand->id,
'brand.image' => 'nullable|max:250|string',
'brand.short_description' => 'nullable|string|max:250',
'brand.description' => 'nullable|string|max:60000',
'brand.sort_order' => 'nullable|integer|min:0|max:10000',
'brand.meta_title' => 'nullable|string|max:250',
'brand.h1' => 'nullable|string|max:250',
'faq.*.question' => 'required|max:250',
'faq.*.answer' => 'required|max:250',
];
}
protected $validationAttributes = [
'faq.*.question' => 'سوال',
'faq.*.answer' => 'جواب',
];
public function addItem()
{
array_push($this->faq, [
'question' => null,
'answer' => null,
]);
}
public function removeItem($index)
{
unset($this->faq[$index]);
}
public function render()
{
return view('livewire.admin.brand.edit');
}
public function update()
{
$this->brand->faq = count($this->faq) ? $this->faq : null;
$this->brand->update();
$this->doneMessage("{$this->brand->name} با موفقیت ویرایش شد");
return redirect()->route('admin.brands.index');
}
public function imageChanged($imagePath)
{
$this->brand->image = ltrim(parse_url($imagePath, PHP_URL_PATH), '/');
}
}