在Laravel中获得主要类别的子类别的麻烦

时间:2017-10-09 13:53:08

标签: php laravel laravel-5

我正在尝试学习Laravel,同时编写一些常用功能。现在我想要做的是当我点击主类别链接打开新页面并显示分配给该类别的所有子类别。听起来很简单但我无法显示它们。

这就是我的分类模型中的内容

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

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

在控制器中

public function categoryListing( $category_id )
{

    $categories = Category::with('children')->get();    

    $category = Category::find($category_id);

    if($category->parent_id == 0) {

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

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

    } else {
        $items =  Item::where('category_id' ,$category_id)->paginate(5);
    }

    return view('category_list', compact('categories','items'));
}

这里的想法是显示该主要类别的主要类别和所有子类别(孩子)。 这是页面上的循环

@foreach($categories as $category)
    <a href="{!!route('list',array($category->id))!!}">
        <span><strong>{!!$category->title!!}</strong> ({!! $category->itemCount!!})</span>
    </a>
    <ul class="list-group">
        @foreach($category as $subcategory)
            <a href="{!!route('list',array($subcategory->id))!!}">{!!$subcategory->title!!}</a>
            <span class="badge badge-primary badge-pill">{!! $subcategory->itemCount !!}</span>
        </li>
        @endforeach
    </ul>
@endforeach

当前错误是

  

尝试获取非对象的属性

在内部的foreach上。

1 个答案:

答案 0 :(得分:0)

试试这个'更干净'..如果有效,请开始添加您的链接和其他内容,这样您就会知道哪些不起作用。

HttpSolrClient server = new 
HttpSolrClient("http://localhost:8983/solr/docs");
            SolrQuery query = new SolrQuery();
            query.setRequestHandler("/analysis/field");
            query.set("analysis.fieldtype", "text_en");
            query.set("analysis.fieldvalue", "TESTS");
            query.set("wt", "json");

确保您尝试打印的变量存在于每个对象中。

您可以在返回{responseHeader={status=0,QTime=2},analysis={field_types={text_en={index={org.apache.lucene.analysis.standard.StandardTokenizer=[{text=TESTS,raw_bytes=[54 45 53 54 53],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1]}],org.apache.lucene.analysis.core.StopFilter=[{text=TESTS,raw_bytes=[54 45 53 54 53],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1]}],org.apache.lucene.analysis.core.LowerCaseFilter=[{text=tests,raw_bytes=[74 65 73 74 73],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1, 1]}],org.apache.lucene.analysis.en.EnglishPossessiveFilter=[{text=tests,raw_bytes=[74 65 73 74 73],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1, 1, 1]}],org.apache.lucene.analysis.miscellaneous.SetKeywordMarkerFilter=[{text=tests,raw_bytes=[74 65 73 74 73],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1, 1, 1, 1],org.apache.lucene.analysis.tokenattributes.KeywordAttribute#keyword=false}],org.apache.lucene.analysis.en.PorterStemFilter=[{text=test,raw_bytes=[74 65 73 74],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1, 1, 1, 1, 1],org.apache.lucene.analysis.tokenattributes.KeywordAttribute#keyword=false}]}}},field_names={}}} 语句之前尝试在控制器中response.getResponse().get("analysis"); 查看@foreach($categories as $category) <span><strong>{!!$category->title!!}</strong></span> <ul class="list-group"> @foreach($category as $subcategory) <li>{!!$subcategory->title!!}</li> @endforeach </ul> @endforeach 内的内容