【问题标题】:Laravel returning children of parents in same tableLaravel 在同一张桌子上返回父母的孩子
【发布时间】:2015-11-14 08:48:58
【问题描述】:

使用 Laravel 5.1,我正在尝试从 MySQL 类别表创建菜单列表。我的服务提供商返回数据,但我不明白如何在 foreach 循环中创建子类别。当我执行循环时,只返回子查询的最后一行。任何指导将不胜感激。

分类表

id  | cat_name      | cat_parent_id
--- | --------------| ------------- 
1   | Parent Cat 1  | NULL 
2   | Parent Cat 2  | NULL 
3   | Child Cat 1   | 2 
4   | Child Cat 2   | 2 
5   | Parent Cat 3  | NULL 
6   | Child Cat 3   | 5

期望的结果

Parent Cat 1
Parent Cat 2
    Child Cat 1
    Child Cat 2
Parent Cat 3
    Child Cat 3

viewComposerServiceProvider.php

public function boot()
{
       $this->composeTopCategoryNavigation();
       $this->composeSubCategoryNavigation();
}

private function composeTopCategoryNavigation()
{
    view()->composer('partials.header', function($view)
        {
            $view->with('top_cats', Category::whereNull('cat_parent_id')->orderBy('cat_name', 'asc')->get());
        });
}

private function composeSubCategoryNavigation()
{
        view()->composer('partials.header', function($view)
        {
            $view->with('sub_cats', Category::whereNotNull('cat_parent_id')->orderBy('cat_name', 'asc')->get());
        });
}

标题视图

<ul>
@foreach ($top_cats as $top_cat)
  <?php $top_cat_slug = str_slug( $top_cat->cat_name, "-"); ?>
  <li>{{ $top_cat->cat_name }}
    @foreach ($sub_cats as $sub_cat)
            @if ( $sub_cat->cat_parent_id === $top_cat->id )
                <ul>
                  <li{{ $sub_cat->cat_name }}</li>
                </ul>
                @endif
        @endforeach
   </li>
@endforeach
</ul>

【问题讨论】:

    标签: php laravel eloquent blade laravel-5.1


    【解决方案1】:

    首先,你所做的事情是低效的。您的视图会遍历每个父类别的所有子类别。如果您正确定义关系并利用 Eloquent 的预加载,您可以更轻松地获取和访问子类别。

    从定义关系开始:

    class Category extends Model {
      //each category might have one parent
      public function parent() {
        return $this->belongsToOne(static::class, 'cat_parent_id');
      }
    
      //each category might have multiple children
      public function children() {
        return $this->hasMany(static::class, 'cat_parent_id')->orderBy('cat_name', 'asc');
      }
    }
    

    一旦你正确定义了关系,你就可以像下面这样获取整个类别树:

    view()->composer('partials.header', function($view) {
      $view->with('categories', Category::with('children')->whereNull('cat_parent_id')->orderBy('cat_name', 'asc')->get());
    });
    

    不需要第二个作曲家,因为父类别已经包含子类别。

    现在,您只需要在视图中显示类别:

    <ul>
      @foreach ($categories as $parent)
        <li>{{ $parent->cat_name }}
          @if ($parent->children->count())
            <ul>
              @foreach ($parent->children as $child)
                <li>{{ $child->cat_name }}</li>
              @endforeach
            </ul>
          @endif
        </li>
      @endforeach
    </ul>
    

    【讨论】:

    • 如果我想拿孩子的一些特定文件怎么办?
    【解决方案2】:

    我自己找到了解决方案。 我必须遍历结果才能访问成员变量。

    @foreach($locations as $location)
                <tr>
                    <td>
                        {{$location->location_id}}
                    </td>
                    <td>
                        @foreach($location->members as $member)
                            {{$member->first_name}}
                        @endforeach
                    </td>
                    <td>
    
                    </td>
                </tr>
            @endforeach
    

    【讨论】:

      猜你喜欢
      • 2020-08-26
      • 2021-03-14
      • 1970-01-01
      • 2020-12-11
      • 2019-01-03
      • 2016-07-25
      • 2018-06-06
      • 2012-08-29
      • 1970-01-01
      相关资源
      最近更新 更多