Laravel Türkiye Discord Kanalı Forumda kod paylaşılırken dikkat edilmesi gerekenler!Birlikte proje geliştirmek ister misiniz?

ozgurzurnaci

  • 3 Kas 2024
  • 23 Ağu 2024 tarihinde katıldı
  • 0 en iyi yanıt
  • Laravel 11'de Livewire ile nested tree kategori sistemi ouşturmaya çalışıyorum. livewire component kodlarım şu şekilde:

    app/Livewire/CategoryTree.php:

    <?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:

    <?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"];
    }

    şu satır sebebiyle hata alıyorum:

    $this->categories = Category::get()->toTree();

    toTree() fonksiyonunu kullandığımda laravel şu hatayı veriyor:

    Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '512' frames

    toFlatTree() fonksiyonunu kullandığımda sayfa sorunsuzca açılıyor ve kategorileri görebiliyorum. Kategorileri toTree() ile sayfaya yazdırmam gerekiyor. Bunu nasıl çözebilirim?