Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sql error when selecting All pages #6650

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,11 +740,18 @@ public function list(Request $request)
$perPage = $request->has('perPage') ? $request->input('perPage') : config('monica.number_of_contacts_pagination');

// search contacts
$contacts = $contacts->search($request->input('search') ?? '', $accountId, 'is_starred', 'desc', $sort)
->paginate($perPage);
$contacts = $contacts->search($request->input('search') ?? '', $accountId, 'is_starred', 'desc', $sort);

if($perPage == -1){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend creating separate methods. It might look like this:

Suggested change
if($perPage == -1){
$total = $this->getTotal($contacts, $perPage);
$contacts = $this->getContacts($contacts, $perPage);

Example of the getTotal() implementation:

private function getTotal($contacts, int $perPage): int
{
    if ($perPage === -1) {
        return $contacts->count();
    }

    return $contacts->paginate($perPage);
}

Please add a type hint for the $contacts argument. The getContacts() method might look similar to the above.

$total = $contacts->count();
$contacts = $contacts->get();
}else{
$contacts = $contacts->paginate($perPage);
$total = $contacts->total();
}

return [
'totalRecords' => $contacts->total(),
'totalRecords' => $total,
'contacts' => ContactResource::collection($contacts),
];
}
Expand Down