Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel 9 – Attempt to read property "nom_prenoms" on null

I have two models as follows.

Tier Model:

public function contact(){
    return $this->belongsTo(ContactAdresse::class,"contact_adresse_id");
}

ContactAdresse Model:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public function tiers(){
    return $this->hasMany(Tier::class);
}

Now I’m trying to retrieve the Tier associated with the Contact using this code:

{{ $ContactAdresse->tier->nom_prenoms}}

I have the following error.

Attempt to read property "nom_prenoms" on null

Does anyone have an idea, please?

>Solution :

If you want to access the nom_prenoms property of each Tier associated with a ContactAdresse, you can loop through the tiers collection like this.

@foreach($ContactAdresse->tiers as $tier)
    {{ $tier->nom_prenoms }}
@endforeach
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading