Laravel ErrorException:从控制器中的空值创建默认对象

Laravel ErrorException:从控制器中的空值创建默认对象,laravel,Laravel,我试图在我的应用程序中运行一些,但没有得到预期的结果,所以我决定通过在我的控制器中的函数内逐行注释代码来跟踪错误的来源。我最后把范围缩小到这一领域: DB::beginTransaction(); try { // Getting mpower transaction record $payment = PaymentTransaction::select('id', 'invoice_reference_code', 'transaction_token')

我试图在我的应用程序中运行一些,但没有得到预期的结果,所以我决定通过在我的控制器中的函数内逐行注释代码来跟踪错误的来源。我最后把范围缩小到这一领域:

DB::beginTransaction();

try
{
    // Getting mpower transaction record
    $payment = PaymentTransaction::select('id', 'invoice_reference_code', 'transaction_token')
        ->where('transaction_token', $transaction_token)
        ->where('is_verified', 0)
        ->first();

    // If found, setting transaction to verified
    $payment->is_verified = 1;
    $payment->save();

    // Getting purchase invoice details
    $invoice_details = InvoiceDetails::where('reference_code', $payment->invoice_reference_code)
        ->where('is_verified', 0)
        ->first();

    // If found, setting invoice to verified
    $invoice_details->is_verified = 1;
    $invoice_details->save();

    DB::commit();
}
catch (\Exception $e)
{
    return [
        'code' => 300,
        'message' => $e->getMessage(),
        'data' => []
    ];
}
引发的错误是:

从空值创建默认对象

错误似乎是在这一行抛出的:

$invoice_details->is_verified = 1;
我已经检查了
$payment
,它确实返回了数据。
我真的不知道我在这里遗漏了什么。

请检查查询是否已返回$invoice\u details的结果。如果返回空,则同样初始化对象:

$invoice_details   = new InvoiceDetails;
然后尝试调用save函数