File: /var/www/admin.fixgini.com/app/Livewire/Category/Update.php
<?php
namespace App\Livewire\Category;
use Livewire\Component;
use App\Models\Category;
use Illuminate\Support\Str;
use Livewire\WithFileUploads;
class Update extends Component
{
use WithFileUploads;
public $categoryId;
public $name = '';
public $icon;
public $description;
public $currentIconUrl;
public $currentThumbnail;
public $new_thumbnail;
public $thumbnail;
public $new_icon_url;
public $categories;
public $subcategories;
protected $rules = [
'name' => 'required|min:5',
'description' => 'required|min:5',
'icon' => 'nullable|image|max:1024', // optional, but if present, should be an image file
];
public function mount($slug)
{
$category = Category::where('slug', $slug)->first();
$this->categories = Category::whereNull('parent_id')->get();
$this->categoryId = $category->id;
// $this->categoryId = $category->parent_id;
$this->name = $category->name;
$this->description = $category->description;
$this->currentIconUrl = $category->icon_url;
$this->currentThumbnail = $category->thumbnail;
}
public function updateCategory()
{
$this->validate();
if ($this->new_icon_url) {
$uploadedFile = $this->new_icon_url->getRealPath();
$uploadResult = cloudinary()->upload($uploadedFile);
$uploadedFileUrl = $uploadResult->getSecurePath();
$publicId = $uploadResult->getPublicId();
}
if ($this->new_thumbnail) {
$uploadedFile = $this->new_thumbnail->getRealPath();
$uploadResult = cloudinary()->upload($uploadedFile, [
'folder' => 'categoryThumbnail',
'transformation' => [
'width' => 260,
'height' => 301,
'crop' => 'crop'
]
]);
$thumbnailuploadedFileUrl = $uploadResult->getSecurePath();
$thumbnailpublicId = $uploadResult->getPublicId();
}
$category = Category::findOrFail($this->categoryId);
$category->update([
'name' => ucwords($this->name),
'description' => $this->description,
'icon_url' => $uploadedFileUrl ?? $category->icon_url,
'icon_public_id' => $publicId ?? $category->icon_public_id,
'thumbnail' => $thumbnailuploadedFileUrl ?? $category->thumbnail,
'thumbnail_public_id' => $thumbnailpublicId ?? $category->thumbnail_public_id,
'slug' => Str::slug($this->name),
]);
session()->flash('message', 'Category successfully updated.');
return redirect()->to('/categories');
}
public function render()
{
return view('livewire.category.update');
}
}