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