2017-09-05 49 views
0

帮助或协助,因为它不会对类别进行排序,我不明白为解决这个问题。 我最近学习laravel。此收集实例上不存在属性[项目]。 laravel 5.4

控制器:class ArticlesController

public function showAll ($category_id) 
{ 
    $categories = Category::where('name', $category_id)->get(); 
    return view('categories')->with(compact('categories', 'articles')); 
} 

这一观点从中我想进入的类别。 site.blade.php

@foreach($categories as $category) 
     <li><a class="nav-link text-white" href={{route('articlesShow', ['id'=>$category->id]) }}">{{ $category->name }}</a></li> 
@endforeach 

路线:

Route::get('/categories/{category_id}', '[email protected]')->name('articlesShow'); 

型号:类别

public function articles() 
{ 
    return $this->hasMany(Article::class, 'category_id'); 
} 

表示在没有得到得到:categories.blade.php

@foreach($categories->articles as $article) 
      <div class="col-6 col-lg-4"> <!--Post --> 
      <img src="{{ asset('upload/image/1.jpg') }}" width="255" alt="..." class="rounded img_size"> 
      <h4> {{ $article->title }}</h4> 
      <h6>Category: <a href="/"> {{ $article->category->name }}</a></h6> 
      <h6>Author: {{ $article->author }}</h6> 
      <p>{{ $article->text }} </p> 
      <p><a class="btn btn-secondary" href="{{ route('articleShow', ['id' =>$article->id]) }}"> More &raquo;</a></p> 
      </div><!--EndPost--> 
@endforeach 

回答

0

$类是包含类别的n数组,并且文章属性属于一个类别,因此在访问文章之前必须循环分类。

试试这个:

@foreach($categories as $category) 
@foreach($category->articles as $article) 
     <div class="col-6 col-lg-4"> <!--Post --> 
     <img src="{{ asset('upload/image/1.jpg') }}" width="255" alt="..." class="rounded img_size"> 
     <h4> {{ $article->title }}</h4> 
     <h6>Category: <a href="/"> {{ $article->category->name }}</a></h6> 
     <h6>Author: {{ $article->author }}</h6> 
     <p>{{ $article->text }} </p> 
     <p><a class="btn btn-secondary" href="{{ route('articleShow', ['id' =>$article->id]) }}"> More &raquo;</a></p> 
     </div><!--EndPost--> 
@endforeach 
@endforeach 
+0

谢谢您花时间帮忙。错误不会产生,但是当你点击链接时,它不会显示任何内容。 –

+0

try {{route('articleShow',['categories_id'=> $ category-> id])}}而不是{{route('articleShow',['id'=> $ article-> id])}} – Quezler

+0

谢谢,现在显示的文章,但在菜单中,当你点击类别时,另一个类别消失,并在它出现的地方它点击。在控制器中的代码是这样的:$ categories = Category :: with('articles') - > where('id',$ category_id) - > get(); return view('categories') - > with(compact('categories',$ categories)); –

0

首先:使用eager loading

假设,你[email protected]方法应该显示给定类别ID的所有文章,您的查询应该是这样的

public function showAll ($category_id) 
{ 
    $categoryArticles = Category::with('articles')->where('id', $category_id)->get(); 
    return view('categories')->with('categoryArticles', $categoryArticles); 
} 

然后以显示给定类别的所有文章(假设一篇文章只有一个类别) ,这可能是一种方式:

@foreach($categoryArticles->articles as $article) 
      <div class="col-6 col-lg-4"> <!--Post --> 
      <img src="{{ asset('upload/image/1.jpg') }}" width="255" alt="..." class="rounded img_size"> 
      <h4> {{ $article->title }}</h4> 
      <h6>Category: <a href="/"> {{ $categoryArticles->name }}</a></h6> 
      <h6>Author: {{ $article->author }}</h6> 
      <p>{{ $article->text }} </p> 
      <p><a class="btn btn-secondary" href="{{ route('articleShow', ['id' =>$article->id]) }}"> More &raquo;</a></p> 
      </div><!--EndPost--> 
@endforeach 
+0

感谢您花时间帮助。 它给出了同样的错误:属性[articles]在这个集合实例中不存在。 (查看:/home/vagrant/Code/nexta/resources/views/categories.blade.php) –

+0

我更新了我的虚拟代码,将变量重命名为'$ categoryArticles',因为您已经在某些地方使用了变量'$ category'以前的刀片模板。这应该现在工作。 – fab

+0

谢谢你,你想帮忙,但你得到这个错误:未定义的变量:categoryArticles(View:/home/vagrant/Code/nexta/resources/views/categories.blade.php) 事实证明,当你去最后的表示没有这个变量,为什么会发生这种情况。 –