0

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.

Peter
  • 2,634
  • 7
  • 32
  • 46
  • What is your problem ? What variables does not exist ? – matiaslauriti Jun 14 '21 at 01:13
  • Had this exact issue. Hundreds of these in an enterprise system. We chose NOT to upgrade PHP on that app. And we're stuck with it. Too much time and expense to go back and fix. Regardless of the 'improvement' to the language, creating a breaking change like this was not well received when we went to exec for resources. Sorry I don't have better news. – Watercayman Jun 14 '21 at 01:23
  • @Watercayman My thoughts on the `improvement` are 100% the same. Thank you. I will let you know if I find a solution somewhere. Best! – Peter Jun 14 '21 at 01:39
  • Apart from the fact that I had never seen `code cleanliness` used as a bad word :D , but can't you just create your own function and override the system one? So you can check for non-existing variables and then run the real function? You can start here https://stackoverflow.com/a/15231403/1529324 – Andrea Olivato Jun 14 '21 at 03:11
  • @AndreaOlivato I will probably give it a try. But in my app I have hundreds of instances for my `compact()` logic. It means dozens of hours unless there is an "one-hack" solution available. – Peter Jun 14 '21 at 06:18
  • According to the [docs](https://www.php.net/manual/en/function.compact) it only throws an `E_NOTICE` which doesn't break your code. – shaedrich Jun 14 '21 at 06:29

1 Answers1

0

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 = [];
Elikill58
  • 4,050
  • 24
  • 23
  • 45
  • Hi and thanks for the answer. It would be great if you could explain to us how and why your code solves the OP's problem as code itself is not always easy to read. – Simas Joneliunas Jan 27 '22 at 06:28