'compact(): Undefined variable: object in Laravel 8

I am now upgrading my Laravel app from 5.8 to 8.

The problem: PHP 7.4 (and 7.3), required for Laravel 8 app, does not accept non-existing variable in the 'compact()'. It'd take hours to rework my code's logic.

In my opinion the problem lies in the design philosophy of the PHP development team. They disregard the fact that php apps built by people like us handle non-existent variables in views or controller logic. Therefore they impose code cleanliness over user-control.

My question:

Is there a solution, such as

  • A lcompact workaround.
  • A crude hack of adding $variable = null; in my controller methods.
  • or hacking the framework code as here https://stackoverflow.com/a/59692651/4209866 (The answer doesn't work in Laravel 8, as the addWhereExistsQuery does not have the operator parameter.)

Thank you.



Solution 1:[1]

This code works fine for laravel 8:

public function check_out() {
    $pesanan = Pesanan::where('user_id', Auth::user()->id)->where('status',0)->first();
    $pesanan_details = [];
    if(!empty($pesanan)) {
        $pesanan_details = PesananDetail::where('pesanan_id', $pesanan->id)->get();
    }

    return view('pesan.check_out', compact('pesanan', 'pesanan_details'));
}

add $pesanan_details = [];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elikill58