{"id":26579658,"url":"https://github.com/lucasdotvin/laravel-database-queries-counter","last_synced_at":"2026-01-11T16:40:02.371Z","repository":{"id":41870136,"uuid":"455918114","full_name":"lucasdotvin/laravel-database-queries-counter","owner":"lucasdotvin","description":"A simple way to check how many queries a test suite has performed.","archived":false,"fork":false,"pushed_at":"2023-10-09T16:05:40.000Z","size":64,"stargazers_count":2,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-02-21T17:16:27.544Z","etag":null,"topics":["database-perfomance","laravel","php","testing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucasdotvin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null},"funding":{"github":"lucasdotvin"}},"created_at":"2022-02-05T16:11:24.000Z","updated_at":"2024-04-25T09:10:21.000Z","dependencies_parsed_at":"2023-02-16T09:15:45.224Z","dependency_job_id":null,"html_url":"https://github.com/lucasdotvin/laravel-database-queries-counter","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasdotvin%2Flaravel-database-queries-counter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasdotvin%2Flaravel-database-queries-counter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasdotvin%2Flaravel-database-queries-counter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasdotvin%2Flaravel-database-queries-counter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucasdotvin","download_url":"https://codeload.github.com/lucasdotvin/laravel-database-queries-counter/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244966457,"owners_count":20539794,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database-perfomance","laravel","php","testing"],"created_at":"2025-03-23T06:18:27.124Z","updated_at":"2026-01-11T16:40:02.286Z","avatar_url":"https://github.com/lucasdotvin.png","language":"PHP","funding_links":["https://github.com/sponsors/lucasdotvin"],"categories":[],"sub_categories":[],"readme":"# Laravel Database Queries Counter\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/lucasdotvin/laravel-database-queries-counter.svg?style=flat-square)](https://packagist.org/packages/lucasdotvin/laravel-database-queries-counter)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/lucasdotvin/laravel-database-queries-counter/run-tests?label=tests)](https://github.com/lucasdotvin/laravel-database-queries-counter/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/lucasdotvin/laravel-database-queries-counter/Check%20\u0026%20fix%20styling?label=code%20style)](https://github.com/lucasdotvin/laravel-database-queries-counter/actions?query=workflow%3A\"Check+%26+fix+styling\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/lucasdotvin/laravel-database-queries-counter.svg?style=flat-square)](https://packagist.org/packages/lucasdotvin/laravel-database-queries-counter)\n\nThis package provides a simple way to check how many queries a test suite has performed.\n\n\u003e It is important to keep in mind that merely controlling how many queries you perform on the database is not enough to ensure your application has a good performance. This package does not intend to do it, instead, it is thought to help you prevent common mistakes (like N+1 queries).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require lucasdotvin/laravel-database-queries-counter --dev\n```\n\nYou can publish the traits if you want to extend them:\n\n```bash\nphp artisan vendor:publish --tag=\"laravel-database-queries-counter-traits\"\n```\n\n## Usage\n\nAdd the `CountsQueries` trait to your test suite class to access the package methods, like `startCountingQueries`, `stopCountingQueries`, and `assertDatabaseQueriesCount`, as demonstrated below, where we assert that an index route does not perform N+1 queries to load the posts from a blog:\n\n```php\n\u003c?php\n\nnamespace Tests\\Feature;\n\nuse App\\Models\\Post;\nuse App\\Models\\User;\nuse LucasDotVin\\DBQueriesCounter\\Traits\\CountsQueries;\nuse Tests\\TestCase;\n\nclass PostTest extends TestCase\n{\n    use CountsQueries;\n\n    public function testIndexPageDoesNotPerfomNPlusOneQueries()\n    {\n        Post::factory()-\u003etimes(10)-\u003ecreate();\n\n        $user = User::factory()-\u003ecreate();\n        $this-\u003eactingAs($user);\n\n        $this-\u003estartCountingQueries();\n\n        $response = $this-\u003eget(route('posts.index'));\n\n        $this-\u003estopCountingQueries();\n\n        $response-\u003eassertSuccessful();\n        $this-\u003eassertDatabaseQueriesCount(1);\n    }\n}\n```\n\nYou can also use the method `whileCountingQueries` to avoid having to control when to start and stop counting queries, as below, where we refactor the example above:\n\n```php\n    public function testIndexPageDoesNotPerfomNPlusOneQueries()\n    {\n        Post::factory()-\u003etimes(10)-\u003ecreate();\n\n        $user = User::factory()-\u003ecreate();\n        $this-\u003eactingAs($user);\n\n        $response = $this-\u003ewhileCountingQueries(fn () =\u003e $this-\u003eget(route('posts.index')));\n\n        $response-\u003eassertSuccessful();\n        $this-\u003eassertDatabaseQueriesCount(1);\n    }\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Lucas Vinicius](https://github.com/lucasdotvin)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucasdotvin%2Flaravel-database-queries-counter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucasdotvin%2Flaravel-database-queries-counter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucasdotvin%2Flaravel-database-queries-counter/lists"}