Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

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.

0

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.

Last updated 9 years ago.
0

If i do this:

echo $p->category()->first()->name;

I can get the category.

But with this I'm losing the eager loading, right?

0

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!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

brunodevel brunodevel Joined 22 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.