【发布时间】:2022-01-12 10:19:18
【问题描述】:
我试图在 try/catch 块中捕获 QueryException 错误。我正在从终端运行migrate:fresh 命令并设置一些配置值。我想排除一些异常并在出现此错误时捕获它。我尝试了所有方法,但似乎无法成功。
命令
php artisan migrate:fresh --seed
错误
Illuminate\Database\QueryException SQLSTATE[08006] [7] 致命: 数据库 "x" 不存在(SQL: select tablename from pg_catalog.pg_tables where schemana*me in ('public'))
代码
try {
DB::purge('pgsql');
Config::set('database.connections.pgsql.database', $config['database']);
Config::set('database.connections.pgsql.username', $config['username']);
Config::set('database.connections.pgsql.password', $config['password']);
} catch (\Throwable $e) {
dd($e); //it never gets here
Log::error($e->getMessage());
}
我也试过了
catch (\Illuminate\Database\QueryException $e)
catch (\Exception $e)
catch (\PDOException $e)
有什么办法吗?
【问题讨论】:
-
php artisan migrate:fresh --seed中是否抛出异常?其余代码与此有何关系? -
你试过
catch (\QueryException $e)吗? -
该命令触发了一个函数,其中我有 try/catch 块。我尝试了 \QueryException 和 \PDOException。
标签: laravel exception eloquent illuminate