0

Here is my department db.It has id name and parent.While using jstree i have displayed the department that has parent 0 with the if condition(this department is the root) now the child has parent the same as the id of the root.I can't find a way to display the children of this root.Thanks in advance. This is my database: enter image description here

  <div id="jstree">
    @foreach($departments as $department)
    <ul>
    @if($department->parent == 0)
      <li>{{ $department->name }}
        <ul>
          <li id="child_node_1">Child node 1</li>
          <li>Child node 2</li>
        </ul>
      </li>
      @endif
    </ul>
    @endforeach
  </div>
  <button>Click</button>

I want it to look like this:

  • Web Development

    • Backend

      • PHP
  • Desktop Dev

UPDATE: this is what I've tried with jQuery DepartmentController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use  App\Department;
use Illuminate\Support\Facades\DB;

class DepartmentController extends Controller
{

    public function treeView(){       
        $departments =  DB::table('departments')->where('parent', '=', 0)->get();

         $tree='<ul id="browser" class="filetree"><li class="tree-view"></li>';
        foreach ($departments as $department) {
             $tree .='<li class="tree-view closed"<a class="tree-name">'.$department->name.'</a>'; //first department
             if(count($department->childs)) {
                $tree .=$this->childView($department);
            }
        }
        $tree .='<ul>';
         return $tree;
        return view('files.treeview',compact('tree'));
    }
    public function childView($department){                 
            $html ='<ul>';
            foreach ($department->childs as $arr) {
                if(count($arr->childs))
                {
                $html .='<li class="tree-view closed"><a class="tree-name">'.$arr->name.'</a>';                  
                        $html.= $this->childView($arr);
                    }
                    else
                    {
                        $html .='<li class="tree-view"><a class="tree-name">'.$arr->name.'</a>';                                 
                        $html .="</li>";
                    }                                   
            }            
            $html .="</ul>";
            return $html;
    } 

and this is what I've written in web.php file

Route::get('/department',array('as'=>'jquery.treeview','uses'=>'Admin\DepartmentController@treeView'));

This is on my Department model

   //category has childs
   public function childs() {
    return $this->hasMany('App\Department','parent','id') ; 
}

but I get this error Undefined property: stdClass::$childs

Romi Halasz
  • 1,949
  • 1
  • 13
  • 23
Coder
  • 131
  • 2
  • 11
  • 1
    You've got a recursive relationship here. This can be quite complicated, as you can't simply do a check for `$department->parent`, as that will only handle 1 nested level. Your initial query for `$departments` should only look for those with a `parent_id` of `null` (or `0` in your case), but you'll need some kind of self-referencing function (recursive) to query for and loop over each "child-of-child" (until there are no more children) – Tim Lewis Feb 18 '20 at 15:17
  • 1
    https://stackoverflow.com/questions/13877656/php-hierarchical-array-parents-and-childs – mitkosoft Feb 18 '20 at 15:18
  • I saw the code from stack overflow now but I have some problems with the function.Im not sure how to write it in laravel.Is there something similar like from this question at stackoverflow but in laravel ? – Coder Feb 18 '20 at 15:24
  • 1
    Laravel uses relationships on Models for Database interaction. You should be able to define `public function parent(){ ... }` and `public function children(){ ... }` on your `Departments.php` model, as demonstrated in https://stackoverflow.com/questions/26652611/laravel-recursive-relationships. Note: You need a `Departments` model; you can't do this efficiently using the `DB::table()` facade. – Tim Lewis Feb 18 '20 at 16:27
  • im sorry i updated the question i added the model Department – Coder Feb 18 '20 at 16:33
  • 1
    read the article http://www.expertphp.in/article/dynamic-treeview-with-jquery-and-laravel-php-framework-example?utm_source=learninglaravel.net – jual ahmed Feb 18 '20 at 16:35
  • @jualahmed this is the article where i was based ,i changed it according to department – Coder Feb 18 '20 at 16:40
  • oh i changed it from DB:: table to Department::where and it was solve thank you very much – Coder Feb 18 '20 at 16:44

0 Answers0