【发布时间】:2013-08-20 17:06:55
【问题描述】:
当我尝试从类别所在的表中获取数据时,我得到未定义的变量:类别错误。
$posts 变量工作正常。
@if(count($posts))
@foreach($posts as $post)
@if($post->category)
<div class="{{ $post->category->name }} isotope-item">
<img src="../img/showcase/1.jpg" alt="">
<div class="disp-post">
<span class="icon"></span>
<p class="time">{{ $post->updated_at }}</p>
<h4>{{ Str::words($post->title, 3) }}</h4>
<p>{{ Str::words($post->body, 60) }}</p>
<p class="link">{{ HTML::linkRoute('posts.show', 'Read more', array($post->id)) }}</p>
</div>
</div>
@endif
@endforeach
@endif
但是当我尝试 $category:
@if(count($category))
@foreach($category as $c)
<li><a href="#" data-filter="{{ $c->name }}">Networking</a></li>
@endforeach
@endif
我收到一个错误。
我做错了什么?
这是来自我的 PostsController
public function showcase()
{
$category = Category::all();
$posts = Post::with('category')->orderBy('updated_at')->get();
return View::make('posts.showcase', compact('posts'));
}
【问题讨论】:
-
您收到错误消息,因为
$category在您的视图中不可用,请尝试return View::make('posts.showcase', compact('posts','category'));
标签: php templates laravel undefined