File: /home/armgroup/libs/app/Http/Livewire/Admin/Category/Edit.php
<?php
namespace App\Http\Livewire\Admin\Category;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Category;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\Rule;
use Livewire\Component;
class Edit extends Component
{
use ConvertEmptyStringsToNull, General;
public $type;
public $categories;
public $category_id;
public $faq;
public Category $category;
protected $listeners = ['imageChanged'];
public function addItem()
{
$this->faq[] = [
'question' => null,
'answer' => null,
];
}
public function removeItem($index)
{
unset($this->faq[$index]);
}
public function mount($type, $categories, $category)
{
$this->categories = $categories;
$this->type = $type;
$this->category_id = $category->id;
$this->faq = $this->category->faq ?: [];
}
protected function rules()
{
return [
'category.name' => 'required|string|max:250',
'category.meta_title' => 'nullable|string|max:250',
'category.h1' => 'nullable|string|max:250',
'category.slug' => ['required', 'string', 'max:250', 'alpha_dash', 'unique:categories,slug,'.$this->category_id],
'category.icon' => 'nullable|max:250|string',
'category.image' => 'nullable|max:250|string',
'category.short_description' => 'nullable|max:250|string',
'category.description' => 'nullable|max:50000|string',
'category.parent_id' => ['nullable', Rule::exists('categories', 'id')
->where(function ($query) {
return $query->where('type', $this->type['className'])
->where('id', '<>', $this->category_id);
})],
'category.status' => 'required|in:0,1',
'category.show_in_index' => 'required|in:0,1',
'category.sort_order' => 'nullable|integer|min:0|max:10000',
'faq.*.question' => 'required|max:250',
'faq.*.answer' => 'required|max:250',
];
}
protected $validationAttributes = [
'faq.*.question' => 'سوال',
'faq.*.answer' => 'جواب',
];
public function update()
{
$this->validate();
$this->category->faq = count($this->faq) ? $this->faq : null;
$this->category->update();
$this->removeCachedCategories();
$this->doneMessage("دستهبندی {$this->category->name} با موفقیت ویرایش شد");
return redirect()->route('admin.categories.showCategories', $this->type['slug']);
}
public function render()
{
return view('livewire.admin.category.edit');
}
public function imageChanged($path, $name)
{
$this->category->{$name} = ltrim(parse_url($path, PHP_URL_PATH), '/');
}
private function removeCachedCategories()
{
Cache::forget('product-categories');
Cache::forget('article-categories');
}
}