I am using Laravel 5.6. I am stuck with the following. The structure of my category model is below.
id | name | cat_parent_id | slug
--- | ------------------| ------------- | -------------
1 | Parent - 1 | NULL | parent-1
2 | Parent - 2 | NULL | parent-2
3 | Child-1- P - 1 | 1 | ch-1-p-1
4 | Child-1- P - 2 | 2 | ch-1-p-2
5 | sCh-1-Ch-1-P- 2 | 4 | sch-1-ch-1-p-2
To obtain children relationship I used the following method on the kblinked\Category model.
public function children()
{
return $this->hasMany('kblinked\Category', 'cat_parent_id', 'id');
}
In my controller,
public function category(Category $category)
{
$categories = $category->first()->children;
return view('product.list', compact('categories'));
}
Here is my route
Route::get('/{category?}','ProductController@category');
I am able to fetch the first children using the following code. It shows the following, when I visit the url, http://trump.localhost/parent-2
Child-1- P - 2
However, it doesn't show anything when I visit http://trump.localhost/parent-2/ch-1-p-2
It should show following, but I don't see it.
sCh-1-Ch-1-P- 2