Files
Paymenter-Version-Tracks/app/Observers/CategoryObserver.php
Muhammad Tamir 85c03cef82 v1.3.4
2025-11-14 10:57:49 +07:00

57 lines
1.4 KiB
PHP

<?php
namespace App\Observers;
use App\Models\Category;
class CategoryObserver
{
/**
* Handle the Category "creating" event.
*
* @return void
*/
public function creating(Category $category)
{
$parent = $category->parent;
$full_slug = $category->slug;
// Set full_slug
while ($parent) {
$fullSlug = $parent->slug;
$full_slug = $fullSlug . '/' . $full_slug;
$parent = $parent->parent;
}
$category->full_slug = $full_slug;
}
/**
* Handle the Category "updating" event.
*
* @return void
*/
public function updating(Category $category)
{
// Did parent or slug change?
if ($category->isDirty('parent_id') || $category->isDirty('slug')) {
$parent = $category->parent;
$full_slug = $category->slug;
// Set full_slug
while ($parent) {
$fullSlug = $parent->slug;
$full_slug = $fullSlug . '/' . $full_slug;
$parent = $parent->parent;
}
$category->full_slug = $full_slug;
// Update children
$category->children->each(function ($child) use ($category) {
$child->update([
'full_slug' => $category->full_slug . '/' . $child->slug,
]);
});
}
}
}