Thirumalai.R raja
Thirumalai.R raja

Reputation: 59

Set limit to eloquent relationship

This is my query.

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

It gives totally 5 records from helps table,but i need each category 5 records of help details. So each category has many helps.

Upvotes: 1

Views: 1181

Answers (1)

Mr. Pyramid
Mr. Pyramid

Reputation: 3935

it's bit late but try my solution for once I hope it will work

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

Upvotes: 3

Related Questions