PHP code example of ericklabs / laravel-query-budget

1. Go to this page and download the library: Download ericklabs/laravel-query-budget 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/ */

    

ericklabs / laravel-query-budget example snippets


use ErickLabs\QueryBudget\Facades\QueryBudget;

QueryBudget::assert(
    maxQueries: 5,
    maxDuplicates: 0,
    callback: fn () => $this->getJson('/api/orders'),
    scope: 'GET /api/orders',
);

use ErickLabs\QueryBudget\Facades\QueryBudget;

public function test_orders_endpoint_respects_query_budget(): void
{
    QueryBudget::assert(
        maxQueries: 8,
        maxDuplicates: 0,
        maxSlowQueries: 1,
        maxTotalDurationMs: 120,
        callback: fn () => $this->getJson('/api/orders'),
        scope: 'GET /api/orders',
    );
}

use ErickLabs\QueryBudget\Data\QueryBudget;
use ErickLabs\QueryBudget\Testing\InteractsWithQueryBudgets;

uses(InteractsWithQueryBudgets::class);

it('lists orders efficiently', function (): void {
    $this->assertQueryBudget(
        budget: new QueryBudget(
            maxQueries: 8,
            maxDuplicates: 0,
        ),
        callback: fn () => $this->getJson('/api/orders'),
        scope: 'GET /api/orders',
    );
});

$result = QueryBudget::measure(
    fn () => $this->getJson('/api/orders')
);

$result->metrics()->queryCount;
$result->metrics()->duplicateQueryCount;
$result->metrics()->totalDurationMs;
$result->metrics()->repeatedPatterns;

QueryBudget::assert(
    maxRepeatedPatterns: 0,
    possibleNPlusOneThreshold: 5,
    callback: fn () => $this->getJson('/api/posts'),
);

return [
    'enabled' => env('QUERY_BUDGET_ENABLED', true),
    'warn_when_disabled' => env('QUERY_BUDGET_WARN_WHEN_DISABLED', true),
    'slow_query_threshold_ms' => (float) env('QUERY_BUDGET_SLOW_QUERY_THRESHOLD_MS', 100),
    'possible_n_plus_one_threshold' => (int) env('QUERY_BUDGET_POSSIBLE_N_PLUS_ONE_THRESHOLD', 5),
    'report_format' => env('QUERY_BUDGET_REPORT_FORMAT', 'console'),
    'connections' => [],
    'ignore' => [],
];

'ignore' => ['telescope_entries', 'migrations', 'sessions'],
'connections' => ['mysql'],

$this->app->bind(QueryReportFormatter::class, MyFormatter::class);

Post::query()
    ->with('author')
    ->get();

$posts = Post::all();

foreach ($posts as $post) {
    $post->author;
}
bash
php artisan vendor:publish --tag=query-budget-config
text
src/
├── Contracts/          # QueryRecorder, QueryNormalizer, QueryReportFormatter
├── Data/               # RecordedQuery, QueryBudget, QueryMetrics, Result, Violation
├── Recording/          # LaravelQueryRecorder, QueryCollection
├── Normalization/      # DefaultQueryNormalizer, QueryFingerprint
├── Analysis/           # Metrics, duplicates, slow queries
├── Budget/             # Evaluator + QueryBudgetExceeded
├── Testing/            # Trait + assertion helper
├── Reporting/          # Console + JSON formatters
├── Facades/QueryBudget.php
├── QueryBudgetManager.php
└── QueryBudgetServiceProvider.php