How to get the pivot value of parent row? #222
-
I have 2 tables, products and products_items. My tree:
My code:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @qcol, |
Beta Was this translation helpful? Give feedback.
-
For descendants on level 2+ (e.g. foreach ($products as $product) {
$parent = $products->where('id', $product->pivot->parentproduct_id)->first();
if ($parent) {
dump($parent->pivot->qty);
}
} This doesn't work for direct children (e.g. |
Beta Was this translation helpful? Give feedback.
For descendants on level 2+ (e.g.
Lev3_item3
ifProduct::find(self::$id)
isLev1_item
), you can find the parent inside$products
:This doesn't work for direct children (e.g.
lev2_item 3
) as their parents aren't part of$products
. Do you also need to get the parent in these cases?