Display Table info with Recursive Relationship
Hi. I'm having hard-time trying to display nested relationship in the table.
For example, these 2 Models:
Category:
public function products() {
return $this->hasMany(Product::class);
}
and
Product:
public function category() {
if ($this->parent) {
//recursive to get the root category
return $this->parent->category();
}
return $this->belongsTo(Category::class);
}
public function parent() {
return $this->belongsTo(Product::class);
}
And then I had:
- Category id = 1 which had Product id = 1 as it's child.
- Product id = 1 which had Product id = 2 as it's child.
I can play around in tinker and extract the category from Product id 2 like:
\App\Models\Product::find(2)->category
, but somehow on the table, the relationship category of Product id 2 return null. My column like: Tables\Columns\TextColumn::make('category.name')
Product 1 show the category just fine, Product 2 just returned null.
How can I fix this? Thank you.0 Replies