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

Wrong resource count when using $query->groupBy() #1209

Closed
robertvrabel opened this issue Jan 8, 2019 · 6 comments
Closed

Wrong resource count when using $query->groupBy() #1209

robertvrabel opened this issue Jan 8, 2019 · 6 comments
Labels
bug Verified bug by the Nova team

Comments

@robertvrabel
Copy link

  • Laravel Version: 5.7.20
  • Nova Version: 1.3.0
  • PHP Version: 7.2

Description:

Using $query->groupBy('id'); displays the incorrect count at the bottom of the resource listing.

Steps To Reproduce:

Use $query->groupBy('id'); in your indexQuery() with a join and you will see the incorrect count.

Fix

I found a solution in nova/src/Http/Controllers/ResourceCountController

Original

return response()->json(['count' => $request->toQuery()->count()]);

Fix

return response()->json(['count' => $request->toQuery()->get()->count()]);

I am not sure if that is the best solution, but I had found it file from this issue: laravel/framework#22883

@bonzai
Copy link

bonzai commented Jan 8, 2019

Duplicate of #625

@bonzai bonzai marked this as a duplicate of #625 Jan 8, 2019
@bonzai bonzai closed this as completed Jan 8, 2019
@robertvrabel
Copy link
Author

@bonzai The issue I filed is quite different than the duplicate you are referring to. I do NOT have a problem with conflicting names. My problem is the resource count at the bottom is incorrect. Refer to this image:

screen shot 2019-01-08 at 12 07 54 pm

@bonzai bonzai marked this as not a duplicate of #625 Jan 8, 2019
@bonzai
Copy link

bonzai commented Jan 8, 2019

If you change Laravel\Nova\Http\Controllers\ResourceCountController::show() to:

public function show(ResourceIndexRequest $request)
{
    $count = $request
        ->toQuery()
        ->toBase()
        ->getCountForPagination();

    return response()->json(['count' => $count]);
}

Will this fix your issue?

Also, please paste your database structure and query from the indexQuery() method.

@bonzai bonzai reopened this Jan 8, 2019
@robertvrabel
Copy link
Author

@bonzai Yes that seems to fix it!

My query is:

"select * from `articles` inner join `application_article` on `id` = `application_article`.`article_id` where `application_article`.`application_id` in (?, ?) and `articles`.`deleted_at` is null group by `id` order by `articles`.`id` desc"

Article table:

CREATE TABLE `articles` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `favicon_id` int(10) unsigned DEFAULT NULL,
  `application_id` int(10) unsigned DEFAULT NULL,
  `title` varchar(255) NOT NULL,
  `short_title` varchar(255) NOT NULL,
  `sub_title` varchar(255) DEFAULT NULL,
  `permalink` varchar(255) NOT NULL,
  `status` varchar(255) NOT NULL,
  `link` varchar(255) DEFAULT NULL,
  `body` text,
  `article_date` date NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `articles_user_id_foreign` (`user_id`),
  KEY `articles_favicon_id_foreign` (`favicon_id`),
  KEY `articles_application_id_foreign` (`application_id`),
  CONSTRAINT `articles_application_id_foreign` FOREIGN KEY (`application_id`) REFERENCES `applications` (`id`),
  CONSTRAINT `articles_favicon_id_foreign` FOREIGN KEY (`favicon_id`) REFERENCES `favicons` (`id`),
  CONSTRAINT `articles_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Application pivot table:

CREATE TABLE `application_article` (
  `application_id` int(10) unsigned NOT NULL,
  `article_id` int(10) unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  KEY `application_article_application_id_index` (`application_id`),
  KEY `application_article_article_id_index` (`article_id`),
  CONSTRAINT `application_article_application_id_foreign` FOREIGN KEY (`application_id`) REFERENCES `applications` (`id`) ON DELETE CASCADE,
  CONSTRAINT `application_article_article_id_foreign` FOREIGN KEY (`article_id`) REFERENCES `articles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

@bonzai bonzai added the bug Verified bug by the Nova team label Jan 8, 2019
@davidhemphill
Copy link
Contributor

This has been fixed and will be included in the next release. 👍

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Verified bug by the Nova team
Projects
None yet
Development

No branches or pull requests

3 participants