3
當我嘗試從表中獲取數據的類別時,我得到未定義變量:類別錯誤。爲什麼我在Laravel視圖中顯示「未定義變量」?
$ posts variable work fine。
@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
但是當我嘗試$類別:
@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'在你看來不可用,試着'返回View :: make('posts.showcase',compact('posts','category'));' –