Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop problem on laravel livewire #604

Open
ozgurzurnaci opened this issue Aug 23, 2024 · 0 comments
Open

Infinite loop problem on laravel livewire #604

ozgurzurnaci opened this issue Aug 23, 2024 · 0 comments

Comments

@ozgurzurnaci
Copy link

ozgurzurnaci commented Aug 23, 2024

I'm trying to create a nested tree category system in laravel livewire. My livewire component codes:

app/Livewire/CategoryTree.php:


use Livewire\Component;
use App\Models\Category;

class CategoryTree extends Component
{
public $categories;
public $newCategoryName = '';
public $parentId = null;

protected $listeners = ['updateOrder' => 'updateOrder'];

public function mount()
{
    $this->categories = Category::get()->toTree();
}

public function render()
{
    return view('livewire.category-tree', ['categories' => $this->categories]);
}

public function addCategory()
{
    $this->validate([
        'newCategoryName' => 'required|string|max:255',
    ]);

    $parent = Category::find($this->parentId);
    $category = new Category(['name' => $this->newCategoryName]);

    if ($parent) {
        $parent->appendNode($category);
    } else {
        Category::create(['name' => $this->newCategoryName]);
    }

    $this->resetForm();
    $this->mount();  // Kategorileri yeniden yükle
}

public function deleteCategory($id)
{
    $category = Category::findOrFail($id);
    $category->delete();
    $this->mount();  // Kategorileri yeniden yükle
}

public function updateOrder($order)
{
    $this->saveOrder($order);
    $this->categories = Category::get()->toTree();
}

private function saveOrder($order, $parentId = null)
{
    foreach ($order as $index => $item) {
        $category = Category::find($item['id']);
        $category->parent_id = $parentId;
        $category->save();

        if (isset($item['children'])) {
            $this->saveOrder($item['children'], $item['id']);
        }
    }

    Category::fixTree();  // lft, rgt, depth değerlerini otomatik olarak düzeltir
}

private function resetForm()
{
    $this->newCategoryName = '';
    $this->parentId = null;
}
}

app/Model/Category.php:


namespace App\Models;
use Kalnoy\Nestedset\NodeTrait;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
use NodeTrait;

protected $fillable = ['name', 'parent_id'];
}

this line cause the problem:
$this->categories = Category::get()->toTree();
when i use the toTree() function, laravel gives this error:
the error is "Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '512' frames"

if i use toFlatTree() function, script runs without error and i can see Categories.

i have to list categories in tree format. how can i fix this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant