PHP code example of mortenscheel / laravel-query-recorder

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

    

mortenscheel / laravel-query-recorder example snippets


use Scheel\QueryRecorder\Facades\QueryRecorder;
use Scheel\QueryRecorder\Processors\CsvProcessor;

class UserController extends Controller
{
    public function index()
    {
        // Create a CSV recorder with a file path
        $recorder = new CsvProcessor(storage_path('queries.csv'));

        // Start recording
        QueryRecorder::record($recorder);

        // Your controller logic with database queries
        $users = User::all();
        
        // The CSV will be written after the response has been sent
        // thanks to Laravel's defer() mechanism
        
        return view('users.index', compact('users'));
    }
}

use Scheel\QueryRecorder\Processors\DuplicateQueryCsvProcessor;

QueryRecorder::record(new DuplicateQueryCsvProcessor('/path/to/output.csv'));

use Scheel\QueryRecorder\RecordsQueries;
use Scheel\QueryRecorder\QueryCollection;

class CustomProcessor implements QueryCollectionProcessor
{
    public function process(QueryCollection $queries): void
    {
        // Custom implementation to record the queries collection
        // This will be called after the request is complete
    }
}

$recorder = new CustomProcessor();
QueryRecorder::record($recorder);

// Execute queries...
DB::table('users')->first();

// The recordQueries method will be called after the response
// has been sent, with all collected queries

$query->origin->file     // The file path where the query was initiated
$query->origin->line     // The line number where the query was initiated
$query->origin->function // The function that initiated the query
$query->origin->class    // The class that initiated the query (if applicable)
$query->origin->type     // The type of call (static or instance method)

// Additional helper methods
$query->origin->isVendor()   // Check if the query originated from a vendor package
$query->origin->location()   // Get the file:line format
$query->origin->editorLink() // Get an editor link to the exact location

use Scheel\QueryRecorder\Facades\QueryRecorder;

QueryRecorder::listen(function (RecordedQuery $query) {
    // Do something with the recorded query
    Log::debug('Query executed', ['origin' => $query->location(), 'sql' => $query->sql]);
});

// Execute database queries...
DB::table('users')->first();