mardi 9 juin 2020

JsTree integration with Laravel

I have product view. I want add to this view category tree. I think about jsTree.

I use in my project Laravel 7 and kalnoy/nestedset and https://www.jstree.com

Mi migration file:

Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('category_name', 155);
            $table->string('description', 155)->nullable();
            $table->string('keywords', 155)->nullable();
            $table->longText('content')->nullable();
            $table->char('enable', 1)->default(0);
            $table->string('photo', 155)->nullable();
            $table->bigInteger('order')->default(0);
            $table->string('slug', 160)->nullable();
            NestedSet::columns($table);
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';
        });

In controller I have:

public function categoryTree(CategoryRepositoryInterface $categoryRepository, Request $request)
    {
        $nodes = $this->getCategoriesTree($categoryRepository->getTree());
        return $nodes;
    }

private function getCategoriesTree($nodes): array
    {
        $categoryArray = array();
        $traverse = function ($categories, $prefix = '-') use (&$traverse, &$categoryArray) {
            foreach ($categories as $category) {
                $categoryArray[] = ['id' => $category->id, 'name' => $prefix . ' ' . $category->category_name];
                $traverse($category->children, $prefix . '-');
            }
        };
        $traverse($nodes);
        return $categoryArray;
    }

And in repository:

public function getTree()
    {
        return $this->model->orderBy('order', 'ASC')->get()->toTree();
    }

My model is Category.

In result I have: https://pastebin.com/uErKGgHP

How can I convert my data to format jsTree?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3cPDgMu
via IFTTT

Aucun commentaire:

Enregistrer un commentaire