File: /var/www/admin.fixgini.com/app/Livewire/Category/Store.php
<?php
namespace App\Livewire\Category;
use Livewire\Component;
use App\Models\Category;
use Illuminate\Support\Str;
use Livewire\WithFileUploads;
class Store extends Component
{
use WithFileUploads;
use WithFileUploads;
public $name;
public $parent_id;
public $description;
public $thumbnail;
public $icon_url;
public $show;
public $categories;
protected $rules = [
'name' => 'required|min:5',
'icon_url' => 'required|image|max:1024',
];
public function mount()
{
$this->categories = Category::whereNull('parent_id')->get();
}
public function saveCategory()
{
try {
$this->validate([
'name' => 'required',
'parent_id' => 'nullable|exists:categories,id',
'icon_url' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:100',
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:100',
'description' => 'nullable|string|max:255',
]);
if ($this->icon_url) {
$uploadedFile = $this->icon_url->getRealPath();
$uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'categoryIcon']);
$IconuploadedFileUrl = $uploadResult->getSecurePath();
$IconpublicId = $uploadResult->getPublicId();
}
if ($this->thumbnail) {
$uploadedFile = $this->thumbnail->getRealPath();
$uploadResult = cloudinary()->upload($uploadedFile, [
'folder' => 'categoryThumbnail',
'transformation' => [
'width' => 260,
'height' => 301,
'crop' => 'crop'
]
]);
$thumbnailuploadedFileUrl = $uploadResult->getSecurePath();
$thumbnailpublicId = $uploadResult->getPublicId();
}
$description = $validatedData['description'] ?? null;
$category = Category::create([
'name' => ucwords($this->name),
'parent_id' => $this->parent_id ?? NULL,
'description' => ucfirst($description),
'icon_url' => $IconuploadedFileUrl ?? NULL,
'icon_public_id' => $IconpublicId ?? NULL,
'thumbnail' => $thumbnailuploadedFileUrl ?? NULL,
'thumbnail_public_id' => $thumbnailpublicId ?? NULL,
'slug' => Str::slug($this->name),
]);
$this->redirect('/categories', navigate: true);
return response()->json(['status' => 'success', 'message' => 'Category created successfully', 'category' => $category], 201);
} catch (\Throwable $th) {
$this->addError('description', $th->getMessage());
}
}
public function render()
{
return view('livewire.category.store');
}
}