priyajey's avatar

Attempt to read property "slug" on null

When going to this url in my site https:/mysite.com/sitemaps.xml I get the following error.

Attempt to read property "slug" on null

File Path: resources / views / default / sitemaps.blade.php

This code is shown in red.

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

Please help

0 likes
13 replies
gych's avatar

The category parameter of the $subcategory variable is NULL, make sure that the relation is loaded.

Share the code where you pass the $subcategory to the view.

priyajey's avatar

@gych

@foreach (App\Models\Subcategories::with(['category:id,slug'])->where('mode', 'on')->get() as $subcategory) {{ url('category', [$subcategory->category->slug, $subcategory->slug]) }} {{$date}} 0.8 @endforeach

gych's avatar

@priyajey Can you share the Subcategories Model

Also format your code so its more readable for us. You can do that by starting with 3 backticks adding your code and close with 3 backticks

Example:

```

YOUR CODE

```

This will display as

YOUR CODE
1 like
priyajey's avatar

@gych

 @foreach (App\Models\Subcategories::with(['category:id,slug'])->where('mode', 'on')->get() as $subcategory)
            <url>
            <loc>{{ url('category', [$subcategory->category->slug, $subcategory->slug]) }}</loc>
            <lastmod>{{$date}}</lastmod>
            <priority>0.8</priority>
            </url>
        @endforeach
gych's avatar

@priyajey Did you correctly add the relation for the category in the Subcategories Model?

Can you share the relation you added to that model.

tykus's avatar

@priyajey either (i) you have not defined a category relation on the Subcategories model, or (ii) one of the Subcategories instances returned from the query does not have an associated Category

priyajey's avatar

@gych

Categories, Subcategories model has the following codes.

Categories model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Categories extends Model
{
	protected $guarded = [];
	public $timestamps = false;

	public function subcategories()
	{
		return $this->hasMany(Subcategories::class, 'category_id')->where('mode', 'on');
	}

}

Subcategories model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Subcategories extends Model
{
	protected $guarded = [];
	public $timestamps = false;

	public function category()
	{
		return $this->belongsTo(Categories::class);
	}
}
priyajey's avatar

I entered many subcategories under different categories and then this error came

only have this error when I try to access the URL example.com/sitemaps.xml

tykus's avatar
tykus
Best Answer
Level 104

@priyajey try to exclude any Subcategories instance(s) that does not have a parent Categories (your class naming is awful.) And really, the query should be handled outside the view template!

 @foreach (App\Models\Subcategories::has('category')->with(['category:id,slug'])->where('mode', 'on')->get() as $subcategory)
1 like
gych's avatar

@priyajey I notice that you are using plural names for the Model names. It is better to follow conventions and use singular names. For example, use 'Category' instead of 'Categories' for Model names.

What is the column name for the category in the table for Subcategories?

1 like
gych's avatar

@priyajey What is the column name used to set a category in the table for Subcategories ?

tykus's avatar

@priyajey great, but do you understand why it worked? You have orphaned Subcategories record(s) which have no valid Categories record associated.

Please or to participate in this conversation.