I'm encountering an issue with Laravel when using PostgreSQL.
When executing a query with a non-existent column, such as Letter::where('aaa', 'dd')->get(); in a scenario where the aaa column does not exist in the letter table, I expect to receive an error message indicating that the column doesn't exist.

但是,对于任何SQL错误,Laravel始终返回错误消息Attempt to read property "id" on null,而不仅仅是缺少列.

我如何配置Laravel,或者调试这个问题来显示特定的SQL错误消息,而不是关于试图读取null上的属性的通用消息?

推荐答案

这应该意味着Laraveltry 访问由于底层SQL错误而尚未实例化的模型实例上的属性.

确保您的Laravel应用程序为debug mode,以获取更详细的错误消息.这可以通过在.env文件中设置APP_DEBUG=true来实现.但是,在生产环境中使用此设置时要谨慎.

判断Laravel中是否启用了logging来捕获所有错误,包括SQL错误.Laravel使用Monolog library,它允许详细的错误记录.

例如,要捕获并记录数据库查询异常:

use Illuminate\Database\QueryException;
use Log;

try {
    $letters = Letter::where('aaa', 'dd')->get();
} catch (QueryException $e) {
    Log::error($e->getMessage());
    // Optionally, return a more user-friendly message or take other appropriate actions
    return response()->json(['error' => 'A database error occurred.'], 500);
}

通过捕捉QueryException,您可以记录详细的错误消息,甚至在响应中返回它以用于调试目的.请注意,在生产环境中公开详细的错误消息可能会带来安全风险.

You can also customize the exception handling in your app/Exceptions/Handler.php file to catch and handle database-related exceptions more transparently.
Modify the render or report methods. That allows you to intercept specific exceptions, such as QueryException, and handle them accordingly.

Handler.php文件的顶部导入必要的异常类:

use Illuminate\Database\QueryException;
use Symfony\Component\HttpFoundation\Response;

report方法用于记录异常.如果需要,您可以对其进行自定义,以添加特定的数据库异常日志(log).该步骤是可选的,并基于您的日志(log)首选项:

public function report(Throwable $exception)
{
    if ($exception instanceof QueryException) {
        // Log the error or send it to an external service like Sentry or Bugsnag
        Log::error($exception->getMessage());
    }

    parent::report($exception);
}

render方法负责将异常转换为发送回客户端的HTTP响应.

public function render($request, Throwable $exception)
{
    // Check if the exception is an instance of QueryException
    if ($exception instanceof QueryException) {
        // Determine if the app is in debug mode
        if (config('app.debug')) {
            // Return a more detailed error response in debug mode
            $errorResponse = [
                'error' => 'Database query error',
                'message' => $exception->getMessage(),
                'sql' => $exception->getSql(),
                'bindings' => $exception->getBindings(),
            ];
            return response()->json($errorResponse, Response::HTTP_INTERNAL_SERVER_ERROR);
        } else {
            // Return a generic error response in production mode
            return response()->json(['error' => 'A database error occurred. Please try again later.'], Response::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    // Process other exceptions as usual
    return parent::render($request, $exception);
}

When a QueryException is caught, you can decide how to respond based on whether your application is in debug mode. In debug mode, you could return detailed error information, including the SQL statement and bindings.
In production mode, you would typically return a generic error message to avoid exposing sensitive information.


the comments年度的OP Hosserin Ibrahim份报告:

My problem is solved. The problem is a line in my custom handler.
I have programed the system to report any exception in telegram, if happen any error in handler no way to report this.
Just solve this line and Laravel return query exceptions in Postgres.

Laravel相关问答推荐

Uncaught ReferenceError:未定义require[Shopify PHP React Browser问题]

Laravel Eloquent查询与集合优化

分页计数(*)查询问题

如何获取多态多对多关系子查询的自动 id 列名?

FileViewFinder.php 第 137 行中的 Laravel 5 InvalidArgumentException:未找到视图 [.admin]

Laravel 5.4 存储:下载文件.文件不存在,但显然存在

Laravel 从现有模型生成迁移

Laravel 5.1 重命名项目

如何从 Windows 命令提示符在 Laravel 中运行 PHPUnit

如何在 Laravel 5 中验证当前、新密码和新密码确认?

如何访问 Carbon 对象类型?

我在哪里放置可以显示 Flash 消息的 Laravel 4 辅助函数?

在 laravel 中安装 vue 3.0

Laravel 在域和子域中共享 cookie 检测问题

Laravel 4 - 一个表单中有两个提交按钮,并且两个提交都由不同的操作处理

如何获取 Laravel 块的返回值?

如何在特定项目中禁用初始化 JS/TS 语言功能?

在 Laravel 5 中找不到类App\Http\Controllers\admin\Auth

错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈:

你如何在 Laravel 5.3 的新通知服务生成的Electron邮件中添加图像?