I have a category table. I also offer sub-categories on this app. I have a foreign key ('parent') on my categories table which refers to the 'id' column on the same table.
My relationship looks like so
public function parentcat()
{
return $this->belongsTo(Category::class, 'parent');
}
When I run Tinker..
$category = \App\Category::find(2)
$category->parentcat->name
It returns the name of the parent category as expected.
In the view; I am outputting the results in a table. If I print
$category->parentcat
I get
// The model object. (Laracasts strips it out though!) Cheers @JeffreyWay :p
But if I add
$category->parentcat->name
I get an exception 'Trying to get property of non-object'
What am I doing wrong?
TIA, Mark