Simplified code
When I put {{ $mea->cat }}
the view returns JSON and "slug" is present.
When I put {{ $mea->cat->slug }}
I have the error: Attempt to read property "slug" on null
WTF 🤪
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Good morning, something really weird.
This code gives me the error Attempt to read property "slug" on null :
@php
// dd(url('cat/'.$mea->cat->slug.'/art/'.$mea->slug));
@endphp
<a href="{{ url('cat/'.$mea->cat->slug.'/art/'.$mea->slug) }}"> {{ $mea->titre }}</a>
If I uncomment "dd", it returns what I want:
"http://lfjp2.test/cat/etablissement-activites-periscolaires/art/les-activites-periscolaires-2023-2024" // resources/views/inc/alaune.blade.php
I'm on new Herd, Laravel 9 up to date, PHP8.2, all caches cleared. Relationships in models OK... I don't understand and can't find the solution :/
Are you looping over a Collection of records; perhaps the first item in the Collection has a cat instance, but another/others do not? Check using @dump directive rather than dd:
@dump($mea->cat);
{{-- <a href="{{ url('cat/'.$mea->cat->slug.'/art/'.$mea->slug) }}"> {{ $mea->titre }}</a> --}}
Please or to participate in this conversation.