user3718908x100
user3718908x100

Reputation: 8529

ErrorException: Creating default object from empty value in Controller

I am trying to run some in my application, i was not getting the expected result, so i decided trace where the error was coming from by commenting out code line by line in my controller, within the function. I finally narrowed it down to this area:

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' => []
    ];
}

The error being thrown is:

Creating default object from empty value

The error seems to be thrown at this line:

$invoice_details->is_verified = 1;

I have checked $payment and it does indeed return data. I really cannot tell what i am missing here.

Upvotes: 2

Views: 1403

Answers (1)

ArtisanBay
ArtisanBay

Reputation: 1041

Please check if the query has returned a result to $invoice_details. If it has returned empty then initialize an object likewise:

$invoice_details   = new InvoiceDetails;

Then try calling the save function.

Upvotes: 3

Related Questions