按与每个类别相关的会议数量排序类别

时间:2018-08-10 10:25:19

标签: php laravel

我正在获得所有类别,例如:

public function index(){
    return view('home')
        ->with('categories', Category::orderBy('created_at', 'desc')->get())
        ->with('conferences', Conference::orderBy('created_at','desc')->where('status','P')->take(8)->get())
}

然后在刀片文件中显示如下类别。但是您知道如何通过与每个类别关联的会议数量来排序类别吗?即,首先显示与更多会议相关联的类别。您知道如何实现吗?

<div class=" d-md-block">
    <div class="container">
        <div class="row">
            <div class="col p-0 m-0">
                <ul class="Categories__Menu">
                    @foreach($categories->take(6) as $category)
                        <li class="">
                            <a href="javascript:void(0)" name="category" id="{{$category->id}}">{{$category->name}}</a>
                        </li>
                    @endforeach
                    <li><a  data-toggle="modal" id="showCategories" data-target=".bd-example-modal-lg" href="">More <i class="fa fa-caret-down" aria-hidden="true"></i></a></li>
                </ul>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

使用它。

Category::withCount('conferences')->orderBy('conferences_count', 'desc')->latest()->get();