doushui20090526 2018-10-22 13:07
浏览 45
已采纳

显示主要类别下的子类别和项目数

I'm new to Laravel and trying to learn a bit while I build "real world" project.

My project is classified site. Currently I'm on step where I should show all categories, sub-categories and number of items under each sub-category.

In the database I story all categories (main and sub) in table called categories. There is column called parent_id which is the ID of the parent category (if is sub-category).

In the model Category.php I have

public function item()
{
    return $this->hasMany('Item','category_id');
}

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

public function getCategories()
{
    $categoires = Category::where('parent_id',0)->get();
    $categoires = $this->addRelation($categoires);
    return $categoires;
}

public function selectChild( $id )
{
    $categoires = Category::where('parent_id',$id)->get();
    $categoires = $this->addRelation($categoires);
    return $categoires;
}

public function addRelation( $categoires )
{

  $categoires->map(function( $item, $key)
  {             
        $sub = $this->selectChild($item->id);
        $item->itemCount = $this->getItemCount($item->id , $item->parent_id );
        return $item = array_add($item, 'subCategory', $sub);
    });
    return $categoires;
}

public function getItemCount( $category_id, $parent_id )
{

    if( $parent_id == 0)
    { // for root-caregory

         $ids = Category::select('id')->where('parent_id', $category_id)->get();
         $array = array();

         foreach ($ids as $id)
         {
            $array[] =  $id->id;
         }

         return Item::whereIn('category_id', $array )->count();
    }
    else
    {
        return Item::where('category_id', $category_id)->count();
    }
}

In my controller

public function index()
{
    $Category = new Category;
    $allCategories = $Category->getCategories();
    return view('frontend.home', compact('allCategories'));
}

In the view this is how I showed the parent categories

@foreach($allCategories as $cats)
     <li><a href="#">{{ $cats->title }}</a>/li>
@endforeach

What I've tried for the sub-categories is to add one if inside the foreach

@foreach($allCategories as $cats)
     <li><a href="#">{{ $cats->title }}</a>
          <ul class="sub-category">
              @if ($cats->children())
                <li><a href="#"> {{ $cats->children->title }} </a></li>
              @endif
          </ul>
     </li>
@endforeach

The error is

Undefined property: Illuminate\Database\Eloquent\Collection::$title

When I {{ dd($allCategories) }} I see that they are in the array.

  • 写回答

1条回答 默认 最新

  • doudg60800 2018-10-22 13:34
    关注

    Your children relation will return a collection and not a single model, because of this you have to loop through the children aswell:

    @foreach($allCategories as $cats)
        <li><a href="#">{{ $cats->title }}</a>
            <ul class="sub-category">
                @if($cats->children)
                    @foreach($cats->children as $child)
                        <li><a href="#"> {{ $child->title }} </a></li>
                    @endforeach
                @endif
            </ul>
        </li>
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)
  • ¥30 notepad++ 自定义代码补全提示
  • ¥15 MATLAB有限差分法解一维边值问题
  • ¥200 内网渗透测试 横向渗透 Windows漏洞 Windows权限维持
  • ¥15 数据结构图的相关代码实现
  • ¥15 python中aiohttp.client_exceptions.ContentTypeError
  • ¥30 DeepLung肺结节检测生成最大froc值对应的epoch报错
  • ¥15 信号发生器如何将频率调大,步尽值改成10
  • ¥15 keil 5 编程智能家具,风扇台灯开关,人体感应等
  • ¥100 找一名渗透方面的专家