douxian3828
2018-04-17 07:03
浏览 10
已采纳

Laravel 5.6嵌套类别

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

图片转代码服务由CSDN问答提供 功能建议

我使用的是Laravel 5.6。 我坚持以下。 我的类别模型的结构如下所示。

  id | 名字|  cat_parent_id |  slu --- \ --- ---  ------------------ |  ------------- |  ------------- 
1 | 家长 -  1 |  NULL |  parent-1 
2 | 家长 -  2 |  NULL |  parent-2 
3 |  Child-1- P  -  1 |  1 |  ch-1-p-1 
4 | 儿童1- P  -  2 |  2 |  ch-1-p-2 
5 |  sCh-1-Ch-1-P-2 |  4 |  sch-1-ch-1-p-2 
   
 
 

为了获得子关系,我在kblinked \ Category模型上使用了以下方法。

  public function children()
 {
返回$ this-> hasMany('kblinked \ Category','cat_parent_id','id'); 
} 
    
 
 

在我的控制器中,

 公共功能类别(类别$类别)
 {
 $ categories = $ category->  first() - > children; 
 
返回视图('product.list',compact('categories')); 
} 
   
 
 

这里 是我的路线

  Route :: get('/ {category?}','ProductController @ category'); 
   
 \  n 

我可以使用以下代码获取第一个孩子。 当我访问网址时,它显示以下内容: http://trump.localhost/parent-2

Child-1- P-2

然而,当我访问 http://trump.localhost/parent-2/ch-1-p-2 \ n

它应该显示如下,但我没有看到它。

sCh-1-Ch-1-P-2

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyi6168 2018-04-21 00:02
    已采纳

    You have to adjust your route:

    Route::get('{category1}/{category2?}/{category3?}/{category4?}',
        'ProductController@category');
    

    And your controller:

    public function category(Category $category1, Category $category2 = null,
            Category $category3 = null, Category $category4 = null) {
        $category = collect(func_get_args())->filter()->last();
    
        $categories = $category->children;
    
        return view('product.list', compact('categories'));
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanji7182 2018-04-17 07:17

    in model

    public function children()
    {
      return $this->hasMany('kblinked\Category', 'cat_parent_id');
    }
    

    in your controller send category id in argument then find that category;

    public function category($category) // $category is id of category
    {
        $category = Category::findOrFail($category);
    
        return view('product.list', compact('categories'));
    }
    

    in your view

    @foreach($category->children as $child)
      {{$child->name}}
    @endforeach
    

    for more documentation

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题