PHP code example of soarestheu / sqlstate-handler

1. Go to this page and download the library: Download soarestheu/sqlstate-handler 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/ */

    

soarestheu / sqlstate-handler example snippets


use Illuminate\Database\QueryException;
use SqlstateHandler\HandlerQueryException;

class StateException extends \Exception
{
    use HandlerQueryException;
}

try {
    DB::table('user')->insert(['id' => 1, 'nome' => null]);
} catch (QueryException $e) {
    throw StateException::fromQueryException($e);
}

$sqlHandler = app('sqlstate.handler');

return [
    'messages' => [
        '23000' => 'Este registro já existe no banco de dados.',
        '23503' => 'Erro de integridade: chave estrangeira inválida.',
        '42000' => 'Erro de sintaxe na query. Verifique seu SQL.',
    ]
];
sh
php artisan vendor:publish --tag=config
json
{
    "sqlstate": "23000",
    "mensagem": "Violação de integridade: dado já cadastrado ou relacionado.",
    "query": "INSERT INTO users (id, email) VALUES (?, ?)",
    "bindings": [1, "[email protected]"],
    "arquivo": "/var/www/html/app/Http/Controllers/StateController.php",
    "linha": 22
}