First for check you can try $p->category, I $p->category is non-object so you can , use method -> to get it property. So $p->category->name will fail.
epetykom said:
First for check you can try $p->category, I $p->category is non-object so you can , use method -> to get it property. So $p->category->name will fail.
epetykom, $p->category is returning the category id from the table.
echo $p->category; // 1
It's a very simple relationship. I really don't know why this is happening.
If i do this:
echo $p->category()->first()->name;
I can get the category.
But with this I'm losing the eager loading, right?
Solved!
I just changed the method name of the relationship to something other than the column name
public function category()
{
return $this->belongsTo('ProductCategory', 'category', 'id');
}
// changing the method name
public function foo()
{
return $this->belongsTo('ProductCategory', 'category', 'id');
}
I guess Eloquent conserves the original attribute, so I need to specify a different method name, to avoid conflicts.
Thanks all!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community