php - 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
解决方案
推荐阅读
- c++ - 随机访问指针向量
- mysql - 如何正确地将两个 JOINS 合并在一起?
- asynchronous - Flutter 如何从异步函数中获取数据
- python - Bokeh DataTable 未显示在 Flask 中
- excel - 如何在excel中将一列映射到另一列
- javascript - 使用特征集合中的坐标来查找质心坐标
- c++ - 如何在不等待新数据到达的情况下使用 boost::asio 的 async_read_some() 读取所有可用数据?
- swift - 如何在swift中从viewController类的静态方法中调用非静态方法?
- jquery - 设置光滑的滑块幻灯片显示
- ansible - 成功响应的 ansible 命令模块结果代码非零