PHP code example of tusharkhan / db-error-explainer-laravel

1. Go to this page and download the library: Download tusharkhan/db-error-explainer-laravel 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/ */

    

tusharkhan / db-error-explainer-laravel example snippets


use Illuminate\Database\QueryException;
use TusharKhan\DbErrorExplainer\Laravel\Facades\DbExplainer;

try {
    User::create(['email' => $email]);
} catch (QueryException $e) {
    $diagnosis = DbExplainer::explain($e);

    $diagnosis->errorType;      // "duplicate_entry"
    $diagnosis->explanation;    // "Duplicate entry: A record with the value ... already exists ..."
    $diagnosis->migrationSuggestion['migration_suggestion'];  // SQL fix, may be empty
    $diagnosis->migrationSuggestion['other_suggestions'];     // Laravel tip, may be empty
}
bash
php artisan db:explain "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'email' cannot be null"
bash
php artisan db:fix-stub "SQLSTATE[HY000]: General error: 1 table items has no column named nope"
# Migration created: database/migrations/2026_10_26_120000_fix_unknown_column.php
bash
php artisan vendor:publish --tag=db-explainer-config