skip to Main Content

I’m encountering an issue in my Laravel application when I try to access the URL https://example.com/sitemaps.xml. The error message I receive is ‘Attempt to read property "slug" on null’.

The error seems to be originating from the following line in my resources/views/default/sitemaps.blade.php file:

{{ url('category', [$subcategory->category->slug, $subcategory->slug]) }}

Can anyone help me understand why this error is occurring and how I can resolve it?

2

Answers


  1. try this

    @if ($subcategory->category)
    {{ url('category', [$subcategory->category->slug, $subcategory->slug]) }}
    @endif
    

    and make sure that $subcategory->category is not NULL

    Login or Signup to reply.
  2. The above error occurs when accessing the slug property from a null category object. Here are two possible solutions:

    For PHP v8 or above

    {{ url('category', [$subcategory->category?->slug, $subcategory->slug]) }}
    

    For PHP v7 or above

    @if ($subcategory->category)
        {{ url('category', [$subcategory->category->slug, $subcategory->slug]) }}
    @endif
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search