如何在 Laravel 中显示所有类别

95媒体

我试图在单个帖子刀片上显示数据库中的所有类别,但是,我也将数据库中的五个类别显示为我的导航菜单。我不确定这可能是我正在使用的循环,因为我不断获得五个类别而不是所有类别。

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());
}

这是我的单个帖子控制器的代码

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 
胡安

我认为您应该在“类别”的索引查询中更改 var 名称。类似的东西->with('categories_to_navigation', Category::take(5)->get())如果视图single.blade.php扩展index.blade.php此 var 名称将被覆盖。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在一个Django模板中显示所有类别

如何在laravel中雄辩地显示每个类别的视频

如何在WordPress中显示所有类别?

如何在Laravel中获取POST的所有输入

如何在WordPress中显示类别列表及其所有帖子?

如何在Laravel 5.3中获取所有视图的列表?

如何在Laravel 5.3中显示图像

如何在Laravel中搜索所有属于标签的文章

Blogdown类别页面显示所有帖子,无论类别如何

如何在所有页面上显示菜单Laravel?

如何在Laravel模型中获取所有记录

如何在语义UI搜索输入中显示所有类别内容的结果?

如何在Laravel中捕获所有请求URL

如何在Laravel中显示模型关系?

如何在Laravel中显示通知?

如何在Laravel中显示GIF

如何在Laravel中合并所有模型?

如何在Laravel中显示所有属于Company的职位

如何使用laravel 5.2获取所有类别的所有产品的计数(即多对多表中的所有记录)

如何在laravel中获取所有月份的记录数

如何在 Laravel 中隐藏空类别

如何在laravel 5.6中使用hasMany对所有记录进行分页和显示?

如何在 Laravel 管理员列表显示中显示帖子的所有者?

Laravel 查询 LeftJoin 显示所有类别,即使在右表中也没有该类别

如何在mysql数据库laravel中查询获取与特定类别相关的所有产品

在mysql中,即使没有数据,我如何在表中显示所有类别?

如何根据类别ID获取laravel中的所有子类别ID?

当所有标签都在产品表中时,如何在 Laravel 中显示产品表中的所有标签?

Laravel - 如何只显示那些有产品的类别?