WST87448735
@WST87448735

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Laravelでカテゴリーごとにレシピの数をCountしたい

解決したいこと

カテゴリー毎にレシピの数を表示させたい

サイドバーにカテゴリー一覧を作り、カテゴリー名の横にそのカテゴリーのレシピが何件あるのかを表示したいです。

各ソースコード

ResipeController
 $categories = Category::all();
 $category_count = Category::withCount('recipes')->take(5)->get();
Category.php
 public function recipes()
    {
        return $this->hasMany('App\Recipe');
    }
index.blade.php
<aside class="aside col-3">
    <div class="widget">
        <span class="widget_title">CATEGORY</span>
        <ul>
            @foreach($categories as $category)
            <li><a href="">{{ $category->name }}</a><span>{{$category_count}}</span></li>
            @endforeach
        </ul>
    </div>
</aside>
0

1Answer

Your answer might help someone💌