jeudi 10 septembre 2020

Laravel hierarchical tree view to display departments

0

Hi I'm tying to build an human resources management system, for the departments I have to display them in a tree view and they have to be hierarchical. When I click a node in the tree view all employees belonging that category should be displayed.I am trying a couple of things but I'm getting an error 404 and it seems I can not find out why. Here is my code please have a look:

Migration

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

Model

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

    public function subcategory(){

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

Controller

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

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

Routes

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');

view admin.departments

@foreach($parentCategories as $department)
 
  

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

view admin.subDepartments

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


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire