首页 > 解决方案 > Laravel 分层树视图显示部门

问题描述

0

嗨,我要建立一个人力资源管理系统,对于部门,我必须在树形视图中显示它们,并且它们必须是分层的。当我单击树视图中的节点时,应显示属于该类别的所有员工。我正在尝试几件事,但我收到错误 404,似乎我无法找出原因。这是我的代码,请看一下:

移民

  Schema::create('departments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->unsignedInteger('parent_id')->nullable();
            $table->timestamps();
        });

模型

class Department extends Model
{
    protected $guarded = [];

    public function subcategory(){

        return $this->hasMany('App\Department', 'parent_id');
    }
}

控制器

class DepartmentController extends Controller
{
    public function index()
    {
        $parentCategories = \App\Department::where('parent_id',0)->get();

        return view('admin.departments', compact('parentCategories'));
    }
}

路线

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/logout', 'HomeController@logout');

Route::get('admin/home', 'HomeController@adminHome')->name('admin.home')->middleware('is_admin');
Route::get('/departments','DepartmentController@index')->name('departments');

查看 admin.departments

@foreach($parentCategories as $department)
 
  {{$department->name}}

  @if(count($department->subcategory))
    @include('admin.subDepartment',['subcategories' => $department->subcategory])
  @endif
 
@endforeach

查看 admin.subDepartments

@foreach($subcategories as $subcategory)
 <ul>
    <li>{{$subcategory->name}}</li> 
  @if(count($subcategory->subcategory))
    @include('admin.subDepartment',['subcategories' => $subcategory->subcategory])
  @endif
 </ul> 
@endforeach

标签: phplaravellaravel-5treeviewhierarchical-data

解决方案


推荐阅读