Skip to content

How to get the pivot value of parent row? #222

Answered by staudenmeir
qcol asked this question in Q&A
Discussion options

You must be logged in to vote

For descendants on level 2+ (e.g. Lev3_item3 if Product::find(self::$id) is Lev1_item), you can find the parent inside $products:

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. lev2_item 3) as their parents aren't part of $products. Do you also need to get the parent in these cases?

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@qcol
Comment options

Comment options

You must be logged in to vote
1 reply
@qcol
Comment options

Answer selected by qcol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants