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

Attempt to read property "name" on null, Laravel 11

I find this error on local development machine. I can see the details of this in the database but when i try to list all the jobs it breaks. So I think it is a something in a way database objects are stored. It gives m error on index.blade.php :

 @foreach ($jobs as $job)
    <article class="space-y-4">
    <a href="jobs/{{$job['id']}}" class="block px-4 py-6 border border-gray-200 rounded-lg">
       <div class="font-bold text-blue-500 text-sm">{{$job->employer->name}}</div>
       <div>
            <li>
              {{ $job['title'] }}: Pays {{$job['salary']}} per year
            </li>
       </div>
    </a>
    </article>
@endforeach
<div>
    {{$jobs->links()}}
</div>

>Solution :

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

I need more information to help you effectively. However, the problem seems to be in the relationship between the Job model and the Employer model. Make sure your Job model is defined like this:

class Job {

 public function employer()
 {
    return $this->belongsTo(Employer::class);
 }

}

Another thing to remember is that $job->employer can be null depending on the relationships in the database. So, it’s important to validate it, like this: {{$job->employer->name ?? ''}}

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