File: /home/armgroup/libs/app/Http/Livewire/Admin/Category/Create.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 Create extends Component
{
use ConvertEmptyStringsToNull, General;
public $type;
public $parent_id;
public $categories;
public $name;
public $slug;
public $icon;
public $image;
public $sort_order;
public $show_in_index = 0;
public $status = 1;
public $description;
public $meta_title;
public $faq = [];
public $h1;
public $short_description;
protected $listeners = ['imageChanged'];
public function updatedName($value)
{
$this->slug = slug($value);
}
public function addItem()
{
$this->faq[] = [
'question' => null,
'answer' => null,
];
}
public function removeItem($index)
{
unset($this->faq[$index]);
}
public function mount($parent, $type, $categories)
{
$this->parent_id = $parent;
$this->categories = $categories;
$this->type = $type;
}
protected function rules()
{
return [
'name' => 'required|string|max:250',
'meta_title' => 'nullable|string|max:250',
'h1' => 'nullable|string|max:250',
'slug' => ['required', 'string', 'max:250', 'alpha_dash', 'unique:categories'],
'icon' => 'nullable|max:250|string',
'image' => 'nullable|max:250|string',
'short_description' => 'nullable|max:250|string',
'description' => 'nullable|max:50000|string',
'parent_id' => ['nullable', Rule::exists('categories', 'id')->where(function ($query) {
return $query->where('type', $this->type['className']);
})],
'status' => 'required|in:0,1',
'show_in_index' => 'required|in:0,1',
'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 store()
{
$data = $this->validate();
$data['faq'] = count($this->faq) ? $this->faq : null;
$data['type'] = $this->type['className'];
Category::create($data);
$this->removeCachedCategories();
$this->doneMessage("دستهبندی {$this->name} با موفقیت ایجاد شد");
return redirect()->route('admin.categories.showCategories', $this->type['slug']);
}
public function render()
{
return view('livewire.admin.category.create');
}
public function imageChanged($path, $name)
{
$this->{$name} = ltrim(parse_url($path, PHP_URL_PATH), '/');
}
private function removeCachedCategories()
{
Cache::forget('product-categories');
Cache::forget('article-categories');
}
}