What version of PHP are you using. In PHP 8, you can start using the null safe operator this:
$invoice?->company?->bank?->amount;
Edited:
After read your question carefully. Indeed you can do so with a "global" solution. Firsts, lets extend the Model class:
class CustomModel extend Model
{
public function __get($key)
{
try {
return $this->getAttribute($key);
} catch (\Exception $exception) {
return null;
}
}
}
Then, pls update all your models to extend from that CustomModel class:
class Invoice extends CustomModel {}
class Company extends CustomModel {}
Still the above solution is a terrible hack, pls don't do that. I don't mind setting aside some time everyday to review and update the whole code base with null safe operators.