File: /home/armgroup/libs/app/Http/Livewire/Admin/Article/Edit.php
<?php
namespace App\Http\Livewire\Admin\Article;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Article;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Component;
use Livewire\WithFileUploads;
class Edit extends Component
{
use ConvertEmptyStringsToNull, General, WithFileUploads;
public Article $article;
public $categories;
public $category_id;
public $image;
public $file_manager_access;
public $can_change_status;
public $faq;
public $productCategories;
public $product_category_id = [];
protected $listeners = ['imageChanged'];
public function addItem()
{
array_push($this->faq, [
'question' => null,
'answer' => null,
]);
}
public function removeItem($index)
{
unset($this->faq[$index]);
}
public function mount($categories, $categoryId)
{
$this->categories = $categories;
$this->category_id = $categoryId;
$this->file_manager_access = auth()->user()->can('file-manager access');
$this->can_change_status = auth()->user()->can('change status');
$this->faq = $this->article->faq ?: [];
$this->productCategories = Category::query()
->where('type', Product::class)
->whereDoesntHave('children')
->get(['id', 'name']);
$this->product_category_id = $this->article->productCategories()->pluck('id')->toArray();
}
protected function rules()
{
return [
'article.title' => 'required|string|max:250',
'article.slug' => 'required|string|max:250|alpha_dash|unique:articles,slug,'.$this->article->id,
'article.image' => 'nullable|max:250|string',
'article.h1' => 'nullable|string|max:250',
'article.description' => 'required|string|max:250',
'article.content' => 'required|string|max:60000',
'article.status' => 'required|in:0,1',
'category_id.*' => ['nullable', Rule::exists('categories', 'id')->where(function ($query) {
return $query->where('type', Article::class);
})],
'product_category_id.*' => ['nullable', Rule::exists('categories', 'id')->where(function ($query) {
return $query->where('type', Product::class);
})],
'image' => 'nullable|mimes:png,jpeg,jpg|max:1024',
'faq.*.question' => 'required|max:250',
'faq.*.answer' => 'required|max:250',
];
}
protected $validationAttributes = [
'faq.*.question' => 'سوال',
'faq.*.answer' => 'جواب',
];
public function render()
{
return view('livewire.admin.article.edit');
}
public function update()
{
$this->validate();
if ((! $this->file_manager_access) && $this->image) {
$folder = getFileManagerSharedFolder().'/'.auth()->id().'/articles';
$this->article->image = 'storage/'.$this->image->store($folder, 'public');
}
if (! $this->can_change_status && $this->article->isDirty()) {
$this->article->status = false;
}
$this->article->faq = count($this->faq) ? $this->faq : null;
DB::transaction(function () {
$this->article->update();
$this->article->categories()->sync($this->category_id);
$this->article->productCategories()->sync($this->product_category_id);
});
$this->doneMessage("{$this->article->title} با موفقیت ویرایش شد");
return redirect()->route('admin.articles.index');
}
public function imageChanged($imagePath)
{
$this->article->image = ltrim(parse_url($imagePath, PHP_URL_PATH), '/');
}
public function nullImage()
{
$this->article->image = $this->image = null;
}
}