【问题标题】:ErrorException: Creating default object from empty value in ControllerErrorException:从控制器中的空值创建默认对象
【发布时间】:2015-10-27 14:34:43
【问题描述】:

我试图在我的应用程序中运行一些程序,但没有得到预期的结果,因此我通过在我的控制器中逐行注释掉函数中的代码来决定跟踪错误的来源。我终于把它缩小到这个区域:

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,它确实返回了数据。 我真的无法说出我在这里缺少什么。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    请检查查询是否已将结果返回到 $invoice_details。如果它返回空,则同样初始化一个对象:

    $invoice_details   = new InvoiceDetails;
    

    然后尝试调用保存函数。

    【讨论】:

      猜你喜欢
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 2020-11-04
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多