Support the ongoing development of Laravel.io →
Eloquent Views Blade

i'm sure this is simple, but need help

i have products, with a foreign key of category_id and i have categories, with an id, and some have a parent_id too

In my blade template, this works no problems {{ $product->category->name }}

But how do i go about showing the parent category name ? So i have {{ $product->category->parent_id }} {{ $product->category->name }} But want to convert my parent_id into the parent name, and am confused how to something like - this is obviously wrong @if($product->category->parent_id){{ $categories[$product->category->parent_id} }}@endif / {{ $product->category->name }}

Last updated 2 years ago.
0

Not quite sure if it is what you want?

{{ isset($this->category->parent_id)?  $categories[$product->category->parent_id] : '' }} / {{ $product->category->name }}
Last updated 2 years ago.
0

duh - yeah exactly, new i was being stupid.

One thing thats now puzzling me is this An example {{ $product->category->parent_id }} = 4

my categories are so #1 1 "Pergolenxy"
#2 2 "Zelten"
#3 3 "Markisen"
#4 4 "Markilux" #5 5 "Side Blinds"

Yet for some reason when i do this $categories[$product->category->parent_id]['name']

I get "Side Blinds" instead of "Markilux", which seems a bit bizarre

  • i'm obviously grabbing the array index(starts at 0) and not the object id

Any suggestions ?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

nousename nousename Joined 23 Apr 2014

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.

© 2025 Laravel.io - All rights reserved.