Question Details

No question body available.

Tags

laravel recursion events tree eloquent-relationship

Answers (1)

March 31, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 60%

Update your model's booted method and remove the manual deleteWithChildren helper:

protected static function booted(): void
{
    static::deleting(function ($category) {
        // 1. Log the current category being deleted
        Log::info('Deleting category: ' . $category->fitle);

// 2. Trigger delete on children. // Use children()->get() to ensure a fresh query/collection // for this specific deletion cycle. $category->children()->get()->each(function ($child) { $child->delete(); // This triggers the 'deleting' event for the child }); }); }

your controller only needs to call a single delete():

public function destroy(Category $category)
{
    // The event takes care of everything recursively
    $category->delete();

return redirect(route('category.index')) ->with('status', ('Category deleted')); }