lundi 28 janvier 2019

How to Display All Categories in Laravel

Am trying to display all the category in the database on single post blade, however, am also display five out the category in the database as my navigation menu. I am not sure maybe its the loop I am using as I keep getting five categories instead of all categories.

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

Here is the code for my single post controller

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

This is how I passed the value in the 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 



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire