如何显示 Laravel 中的所有类别
How to Display All Categories in Laravel
我试图在单个 post blade 上显示数据库中的所有类别,但是,我还在数据库中显示五个类别作为我的导航菜单。我不确定它是否是我正在使用的循环,因为我不断获得五个类别而不是所有类别。
public function index()
{
return view('index')->with('title', Setting::first()->site_name)
->with('categories', Category::take(5)->get())
->with('first_post', Post::orderBy('created_at', 'desc')->first())
->with('second_post', Post::orderBy('created_at', 'desc')->skip(1)->take(1)->get()->first())
->with('third_post', Post::orderBy('created_at', 'desc')->skip(2)->take(1)->get()->first())
->with('wordpress', Category::find(4))
->with('laravel', Category::find(3))
->with('settings', Setting::first());
}
这是我的单个 post 控制器的代码
public function singlePost($slug)
{
$post = Post::where('slug', $slug)->first();
$next_id = Post::where('id', '>', $post->id)->min('id');
$prev_id = Post::where('id', '<', $post->id)->max('id');
return view('single')->with('post', $post)
->with('title', $post->title)
->with('settings', Setting::first())
->with('categories', Category::all())
->with('next', Post::find($next_id))
->with('prev', Post::find($prev_id))
->with('tags', Tag::all())
->with('first_post', Post::orderBy('created_at', 'desc')->first())
->with('second_post', Post::orderBy('created_at', 'desc')->skip(1)->take(1)->get()->first())
->with('third_post', Post::orderBy('created_at', 'desc')->skip(2)->take(1)->get()->first());
}
这就是我在 single.blade.php
中传递值的方式
@foreach($categories as $category)
<div class="post-category-wrap">
<div class="category-post-item">
<!-- <span class="post-count">168</span> -->
<a href="{{route('category.single', ['id' => $category->slug])}}" class="category-title">{{$category->name}}
<i class="seoicon-right-arrow"></i>
</a>
</div>
</div>
@endforeach
我认为您应该更改 'categories' 的索引查询中的 var 名称。就像是
->with('categories_to_navigation', Category::take(5)->get())
。
如果视图 single.blade.php
扩展 index.blade.php
此 var 名称将被覆盖。
我试图在单个 post blade 上显示数据库中的所有类别,但是,我还在数据库中显示五个类别作为我的导航菜单。我不确定它是否是我正在使用的循环,因为我不断获得五个类别而不是所有类别。
public function index()
{
return view('index')->with('title', Setting::first()->site_name)
->with('categories', Category::take(5)->get())
->with('first_post', Post::orderBy('created_at', 'desc')->first())
->with('second_post', Post::orderBy('created_at', 'desc')->skip(1)->take(1)->get()->first())
->with('third_post', Post::orderBy('created_at', 'desc')->skip(2)->take(1)->get()->first())
->with('wordpress', Category::find(4))
->with('laravel', Category::find(3))
->with('settings', Setting::first());
}
这是我的单个 post 控制器的代码
public function singlePost($slug)
{
$post = Post::where('slug', $slug)->first();
$next_id = Post::where('id', '>', $post->id)->min('id');
$prev_id = Post::where('id', '<', $post->id)->max('id');
return view('single')->with('post', $post)
->with('title', $post->title)
->with('settings', Setting::first())
->with('categories', Category::all())
->with('next', Post::find($next_id))
->with('prev', Post::find($prev_id))
->with('tags', Tag::all())
->with('first_post', Post::orderBy('created_at', 'desc')->first())
->with('second_post', Post::orderBy('created_at', 'desc')->skip(1)->take(1)->get()->first())
->with('third_post', Post::orderBy('created_at', 'desc')->skip(2)->take(1)->get()->first());
}
这就是我在 single.blade.php
中传递值的方式@foreach($categories as $category)
<div class="post-category-wrap">
<div class="category-post-item">
<!-- <span class="post-count">168</span> -->
<a href="{{route('category.single', ['id' => $category->slug])}}" class="category-title">{{$category->name}}
<i class="seoicon-right-arrow"></i>
</a>
</div>
</div>
@endforeach
我认为您应该更改 'categories' 的索引查询中的 var 名称。就像是
->with('categories_to_navigation', Category::take(5)->get())
。
如果视图 single.blade.php
扩展 index.blade.php
此 var 名称将被覆盖。