Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

usamajalal's avatar

Laravel relation Trying to get property 'id' of non-object exception handeling

In my Laravel app there are lots of and different types of relations defined over models, but in view there is a mistake, when ever some attributes are used which are in related object (like $invoice->company->name) there is no check for if that relation exist or like we get that object or not, it should be like this:

if($invoice->company){ {{$invoice->company->name}} }

but I skipped that check and directly accessing that attribute, the problem is that it is not in one relation, it is in every relation and I have used that at a lot of places so editing and adding check every where is not possible

I tried this: In relation , I added a check that will check if object is returned, if not that return a default dummy object, but that is not applicable as I have to do that same in all relations definition in all models because that will fail in case of something like this:

{{$invoice->company->bank->amount}}

because in this case amount will throw that error.

I want to find a global solution, that I will create once but it handle all , I tried to make an exception handler that if I get error like Trying to get property of non-object then instead of error, just print N/A. a single modification in base model may be or a single exception will be very helpful.

0 likes
10 replies
kevinbui's avatar

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.

1 like
usamajalal's avatar

@kevinbui I mentioned in my question that I cant edit these, there are thousands of places where I have to edit this, thats why I am looking for a global exception

usamajalal's avatar

@kevinbui thanks, BTW I have done this: use Eloquent as Model; class Quotation extends Model implements HasMedia so in customModel I will extand Eloquent ? and in mu class, I will just do like this: class Quotation extends customModel implements HasMedia am I right?

1 like
krisi_gjika's avatar

@kevinbui would catch (\Exception $exception) work? as I think calling an attribute on non-object is an Error not Exception

1 like
usamajalal's avatar

@Snapey well not thousands :) , I just wanted to say a lot, Actually its a huge code base and this project was shifted to me and I discovered this, and I was surprised too that no one has reported this. PS: Sorry for the late reply, I was away for a month. And that project is now shifted to another group.

johndivam's avatar

Just use withDefault() on your model relation function . In Laravel, the withDefault method is used to provide default values for relationships if the related model doesn't exist. This can be particularly useful to avoid null-related errors when accessing properties or methods on a non-existing related model.

example :

class User extends Model
{
    public function profile()
    {
        return $this->hasOne(Profile::class)->withDefault();
    }
}
3 likes

Please or to participate in this conversation.