PHP code example of lucasdotvin / laravel-database-queries-counter

1. Go to this page and download the library: Download lucasdotvin/laravel-database-queries-counter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lucasdotvin / laravel-database-queries-counter example snippets




namespace Tests\Feature;

use App\Models\Post;
use App\Models\User;
use LucasDotVin\DBQueriesCounter\Traits\CountsQueries;
use Tests\TestCase;

class PostTest extends TestCase
{
    use CountsQueries;

    public function testIndexPageDoesNotPerfomNPlusOneQueries()
    {
        Post::factory()->times(10)->create();

        $user = User::factory()->create();
        $this->actingAs($user);

        $this->startCountingQueries();

        $response = $this->get(route('posts.index'));

        $this->stopCountingQueries();

        $response->assertSuccessful();
        $this->assertDatabaseQueriesCount(1);
    }
}

    public function testIndexPageDoesNotPerfomNPlusOneQueries()
    {
        Post::factory()->times(10)->create();

        $user = User::factory()->create();
        $this->actingAs($user);

        $response = $this->whileCountingQueries(fn () => $this->get(route('posts.index')));

        $response->assertSuccessful();
        $this->assertDatabaseQueriesCount(1);
    }
bash
php artisan vendor:publish --tag="laravel-database-queries-counter-traits"