Set limit to eloquent relationship

这是我的查询。

$helpCategoryList = HelpCategory::where('is_active', 1)
                               ->with(['helps' => function($query) {
            $query->with(['users'])
                  ->withcount(['helpComments','helpResponse','helpVotes'])
                  ->limit(5);}])
           ->orderBy('created_at', 'asc')
           ->get()
           ->toArray();

授予来自帮助表的5个记录,但我需要每个5类的帮助细节记录。所以每个类别都有很多帮助。

回答 1

  1. 赞同 3

    迟到了,但试试我的解决方法,希望它能工作

    $helpCategoryList = HelpCategory::where('is_active', 1)
                                ->with('helps')->whereHas('helps',function($query) {
                                        $query->with(['users'])
                                        ->withcount(['helpComments','helpResponse','helpVotes'])
                                          ->take(5);})
                                ->orderBy('created_at', 'asc')
                                ->get()
                                ->toArray();
    

    Mr 金字塔